Unverified Commit 52ae1f8a authored by Shu Ding's avatar Shu Ding Committed by GitHub

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 b7b7f973
......@@ -2983,12 +2983,13 @@ export default async function build(
if (i18n) {
if (additionalSsgFile) return
const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.slice(
'pages/'.length
)
for (const locale of i18n.locales) {
const curPath = `/${locale}${page === '/' ? '' : page}`
const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.slice(
'pages/'.length
)
if (isSsg && ssgNotFoundPaths.includes(curPath)) {
continue
......
......@@ -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(
<>
{
......@@ -69,16 +81,21 @@ export function makeGetServerInsertedHTML({
return <script key={polyfill.src} {...polyfill} />
})
}
{renderServerInsertedHTML()}
{serverInsertedHTML}
{errorMetaTags}
</>
</>,
{
// Larger chunk because this isn't sent over the network.
// Let's set it to 1MB.
progressiveChunkSize: 1024 * 1024,
}
)
hasUnflushedPolyfills = false
// Wait for the stream to be ready.
await stream.allReady
// There's no need to wait for the stream to be ready
// e.g. calling `await stream.allReady` because `streamToString` will
// wait and decode the stream progressively with better parallelism.
return streamToString(stream)
}
}
......@@ -195,25 +195,33 @@ function createHeadInsertionTransformStream(
}
const insertion = await insert()
const encodedInsertion = encoder.encode(insertion)
if (inserted) {
controller.enqueue(encodedInsertion)
if (insertion) {
const encodedInsertion = encoder.encode(insertion)
controller.enqueue(encodedInsertion)
}
controller.enqueue(chunk)
freezing = true
} else {
// 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)
if (index !== -1) {
const insertedHeadContent = new Uint8Array(
chunk.length + encodedInsertion.length
)
insertedHeadContent.set(chunk.slice(0, index))
insertedHeadContent.set(encodedInsertion, index)
insertedHeadContent.set(
chunk.slice(index),
index + encodedInsertion.length
)
controller.enqueue(insertedHeadContent)
if (insertion) {
const encodedInsertion = encoder.encode(insertion)
const insertedHeadContent = new Uint8Array(
chunk.length + encodedInsertion.length
)
insertedHeadContent.set(chunk.slice(0, index))
insertedHeadContent.set(encodedInsertion, index)
insertedHeadContent.set(
chunk.slice(index),
index + encodedInsertion.length
)
controller.enqueue(insertedHeadContent)
} else {
controller.enqueue(chunk)
}
freezing = 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