Commit c262f388 authored by Shu Ding's avatar Shu Ding Committed by Zack Tanner

Improve rendering performance (#64408)

This PR improves the server rendering performance a bit, especially for
the App Router. It has mainly 2 changes.

A rough benchmark test with a 300kB Lorem Ipsum page, SSR is about 18%
faster.

### Improve `getServerInsertedHTML`

- Avoid an extra `renderToReadableStream` if we already know the content
will be empty
- Avoid `await stream.allReady` for better parallelism with
`streamToString()`
- Increase the `progressiveChunkSize`

### Improve `createHeadInsertionTransformStream`

- Only do chunk splitting and enqueuing if the inserted string is not
empty
parent cb13dc8a
...@@ -2981,12 +2981,13 @@ export default async function build( ...@@ -2981,12 +2981,13 @@ export default async function build(
if (i18n) { if (i18n) {
if (additionalSsgFile) return if (additionalSsgFile) return
const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.slice(
'pages/'.length
)
for (const locale of i18n.locales) { for (const locale of i18n.locales) {
const curPath = `/${locale}${page === '/' ? '' : page}` const curPath = `/${locale}${page === '/' ? '' : page}`
const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.slice(
'pages/'.length
)
if (isSsg && ssgNotFoundPaths.includes(curPath)) { if (isSsg && ssgNotFoundPaths.includes(curPath)) {
continue continue
......
...@@ -60,6 +60,18 @@ export function makeGetServerInsertedHTML({ ...@@ -60,6 +60,18 @@ export function makeGetServerInsertedHTML({
} }
} }
const serverInsertedHTML = renderServerInsertedHTML()
// Skip React rendering if we know the content is empty.
if (
!hasUnflushedPolyfills &&
errorMetaTags.length === 0 &&
Array.isArray(serverInsertedHTML) &&
serverInsertedHTML.length === 0
) {
return ''
}
const stream = await renderToReadableStream( const stream = await renderToReadableStream(
<> <>
{ {
...@@ -69,16 +81,21 @@ export function makeGetServerInsertedHTML({ ...@@ -69,16 +81,21 @@ export function makeGetServerInsertedHTML({
return <script key={polyfill.src} {...polyfill} /> return <script key={polyfill.src} {...polyfill} />
}) })
} }
{renderServerInsertedHTML()} {serverInsertedHTML}
{errorMetaTags} {errorMetaTags}
</> </>,
{
// Larger chunk because this isn't sent over the network.
// Let's set it to 1MB.
progressiveChunkSize: 1024 * 1024,
}
) )
hasUnflushedPolyfills = false hasUnflushedPolyfills = false
// Wait for the stream to be ready. // There's no need to wait for the stream to be ready
await stream.allReady // e.g. calling `await stream.allReady` because `streamToString` will
// wait and decode the stream progressively with better parallelism.
return streamToString(stream) return streamToString(stream)
} }
} }
...@@ -195,25 +195,33 @@ function createHeadInsertionTransformStream( ...@@ -195,25 +195,33 @@ function createHeadInsertionTransformStream(
} }
const insertion = await insert() const insertion = await insert()
const encodedInsertion = encoder.encode(insertion)
if (inserted) { if (inserted) {
controller.enqueue(encodedInsertion) if (insertion) {
const encodedInsertion = encoder.encode(insertion)
controller.enqueue(encodedInsertion)
}
controller.enqueue(chunk) controller.enqueue(chunk)
freezing = true freezing = true
} else { } else {
// TODO (@Ethan-Arrowood): Replace the generic `indexOfUint8Array` method with something finely tuned for the subset of things actually being checked for. // TODO (@Ethan-Arrowood): Replace the generic `indexOfUint8Array` method with something finely tuned for the subset of things actually being checked for.
const index = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.HEAD) const index = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.HEAD)
if (index !== -1) { if (index !== -1) {
const insertedHeadContent = new Uint8Array( if (insertion) {
chunk.length + encodedInsertion.length const encodedInsertion = encoder.encode(insertion)
) const insertedHeadContent = new Uint8Array(
insertedHeadContent.set(chunk.slice(0, index)) chunk.length + encodedInsertion.length
insertedHeadContent.set(encodedInsertion, index) )
insertedHeadContent.set( insertedHeadContent.set(chunk.slice(0, index))
chunk.slice(index), insertedHeadContent.set(encodedInsertion, index)
index + encodedInsertion.length insertedHeadContent.set(
) chunk.slice(index),
controller.enqueue(insertedHeadContent) index + encodedInsertion.length
)
controller.enqueue(insertedHeadContent)
} else {
controller.enqueue(chunk)
}
freezing = true freezing = true
inserted = true inserted = true
} }
......
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