Unverified Commit 70a53e07 authored by Shu Ding's avatar Shu Ding Committed by GitHub

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)
parent 82801143
......@@ -970,7 +970,10 @@ export class NextScript extends Component<OriginProps> {
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) {
......
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 = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment