From 70a53e0789c7e361f12139db6e124a5bb1d2afd9 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 13 Jul 2022 01:39:18 +0200 Subject: [PATCH] Optimize Edge SSR bundle size (#38570) Both `Buffer` and `node-html-parser` are not necessary to be introduced to the Edge SSR bundle. This makes the bundle size 25% smaller. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) --- packages/next/pages/_document.tsx | 5 ++++- packages/next/server/post-process.ts | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/next/pages/_document.tsx b/packages/next/pages/_document.tsx index b9173daf056c..3086c431568c 100644 --- a/packages/next/pages/_document.tsx +++ b/packages/next/pages/_document.tsx @@ -970,7 +970,10 @@ export class NextScript extends Component { const { __NEXT_DATA__, largePageDataBytes } = context try { const data = JSON.stringify(__NEXT_DATA__) - const bytes = Buffer.from(data).byteLength + const bytes = + process.env.NEXT_RUNTIME === 'edge' + ? new TextEncoder().encode(data).buffer.byteLength + : Buffer.from(data).byteLength const prettyBytes = require('../lib/pretty-bytes').default if (largePageDataBytes && bytes > largePageDataBytes) { diff --git a/packages/next/server/post-process.ts b/packages/next/server/post-process.ts index 330172e9e678..0c802b855b17 100644 --- a/packages/next/server/post-process.ts +++ b/packages/next/server/post-process.ts @@ -1,5 +1,6 @@ import type { RenderOpts } from './render' -import { parse, HTMLElement } from 'next/dist/compiled/node-html-parser' +import type { HTMLElement } from 'next/dist/compiled/node-html-parser' + import { OPTIMIZED_FONT_PROVIDERS } from '../shared/lib/constants' import { nonNullable } from '../lib/non-nullable' @@ -7,11 +8,15 @@ let optimizeAmp: typeof import('./optimize-amp').default | undefined let getFontDefinitionFromManifest: | typeof import('./font-utils').getFontDefinitionFromManifest | undefined +let parse: typeof import('next/dist/compiled/node-html-parser').parse if (process.env.NEXT_RUNTIME !== 'edge') { optimizeAmp = require('./optimize-amp').default getFontDefinitionFromManifest = require('./font-utils').getFontDefinitionFromManifest + parse = ( + require('next/dist/compiled/node-html-parser') as typeof import('next/dist/compiled/node-html-parser') + ).parse } type postProcessOptions = {