Unverified Commit 523ae09b authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[Cache Components] Faster partial hydration in PPR resumes (#82742)

This PR fixes some HTML/RSC stream interleaving logic that, under some
circumstances, resulted in hydration of a resumed page being delayed
unnecessarily.

Fixes NAR-284

---

When SSRing a page, we're mixing two streams -- the HTML stream, and the
RSC stream (containing scripts with RSC data, used for hydration). In a
regular (non-PPR) render, we need to wait for react to flush the first
HTML chunk (containing `<html><body>...`) before we can send the first
RSC script. This was implemented in `createMergedTransformStream`.

The problem was that `createMergedTransformStream` was also re-used when
rendering a PPR `resume()`. In that case, we're only outputting the
dynamic portion of HTML, and the shell is sent separately (either by the
next server, outside of `app-render`, or by infrastructure), meaning
that is _not_ part of the stream that `createMergedTransformStream`
receives. But the "wait for the first html chunk" logic was still there,
and thus, we were accidentally delaying sending any RSC scripts until
the first piece of dynamic HTML was rendered even if they had no
connection to each other. In particular, this also prevented us from
sending hydration data for the static shell.

There's one edge case here -- if we didn't produce a static shell (e.g.
for suspense-above-body), we should still apply the html-waiting logic,
otherwise we'd end up sending scripts before the body is rendered. But
if we have a shell, we shouldn't wait for the HTML at all.

This is now solved as follows:
1. During the prerender, we track whether or not a shell (aka "prelude")
was produced, and store that information in the postponed state object
2. When resuming, we check whether the prerender had a shell, and if it
does, we don't wait for HTML (under the assumption that it was already
sent separately, outside the dynamic render)

To test this, I've had to extend our testing setup a bit -- with the
default settings, playwright would wait until `load` is fired, which
seems to fire after all the HTML finished streaming and thus doesn't let
us inspect whether partial hydration is working correctly. We're also
waiting for `load` in `elementByCss` (apparently, for compatibility with
tests written before playwright) so i've had to work around that as
well.
parent c970d951
...@@ -107,6 +107,7 @@ import { ...@@ -107,6 +107,7 @@ import {
import { import {
DynamicState, DynamicState,
type PostponedState, type PostponedState,
DynamicHTMLPreludeState,
parsePostponedState, parsePostponedState,
} from './postponed-state' } from './postponed-state'
import { import {
...@@ -2291,8 +2292,8 @@ async function renderToStream( ...@@ -2291,8 +2292,8 @@ async function renderToStream(
) )
} else if (postponedState) { } else if (postponedState) {
// We assume we have dynamic HTML requiring a resume render to complete // We assume we have dynamic HTML requiring a resume render to complete
const postponed = getPostponedFromState(postponedState) const { postponed, preludeState } =
getPostponedFromState(postponedState)
const resume = ( const resume = (
require('react-dom/server') as typeof import('react-dom/server') require('react-dom/server') as typeof import('react-dom/server')
).resume ).resume
...@@ -2319,6 +2320,12 @@ async function renderToStream( ...@@ -2319,6 +2320,12 @@ async function renderToStream(
tracingMetadata: tracingMetadata, tracingMetadata: tracingMetadata,
}) })
return await continueDynamicHTMLResume(htmlStream, { return await continueDynamicHTMLResume(htmlStream, {
// If the prelude is empty (i.e. is no static shell), we should wait for initial HTML to be rendered
// to avoid injecting RSC data too early.
// If we have a non-empty-prelude (i.e. a static HTML shell), then it's already been sent separately,
// so we shouldn't wait for any HTML to be emitted from the resume before sending RSC data.
delayDataUntilFirstHtmlChunk:
preludeState === DynamicHTMLPreludeState.Empty,
inlinedDataStream: createInlinedDataReadableStream( inlinedDataStream: createInlinedDataReadableStream(
reactServerResult.consume(), reactServerResult.consume(),
nonce, nonce,
...@@ -3919,6 +3926,9 @@ async function prerenderToStream( ...@@ -3919,6 +3926,9 @@ async function prerenderToStream(
// Dynamic HTML case // Dynamic HTML case
metadata.postponed = await getDynamicHTMLPostponedState( metadata.postponed = await getDynamicHTMLPostponedState(
postponed, postponed,
preludeIsEmpty
? DynamicHTMLPreludeState.Empty
: DynamicHTMLPreludeState.Full,
fallbackRouteParams, fallbackRouteParams,
resumeDataCache resumeDataCache
) )
...@@ -4071,27 +4081,28 @@ async function prerenderToStream( ...@@ -4071,27 +4081,28 @@ async function prerenderToStream(
const prerender = ( const prerender = (
require('react-dom/static') as typeof import('react-dom/static') require('react-dom/static') as typeof import('react-dom/static')
).prerender ).prerender
const { prelude, postponed } = await workUnitAsyncStorage.run( const { prelude: unprocessedPrelude, postponed } =
ssrPrerenderStore, await workUnitAsyncStorage.run(
prerender, ssrPrerenderStore,
<App prerender,
reactServerStream={reactServerResult.asUnclosingStream()} <App
preinitScripts={preinitScripts} reactServerStream={reactServerResult.asUnclosingStream()}
clientReferenceManifest={clientReferenceManifest} preinitScripts={preinitScripts}
ServerInsertedHTMLProvider={ServerInsertedHTMLProvider} clientReferenceManifest={clientReferenceManifest}
nonce={nonce} ServerInsertedHTMLProvider={ServerInsertedHTMLProvider}
/>, nonce={nonce}
{ />,
onError: htmlRendererErrorHandler, {
onHeaders: (headers: Headers) => { onError: htmlRendererErrorHandler,
headers.forEach((value, key) => { onHeaders: (headers: Headers) => {
appendHeader(key, value) headers.forEach((value, key) => {
}) appendHeader(key, value)
}, })
maxHeadersLength: reactMaxHeadersLength, },
bootstrapScripts: [bootstrapScript], maxHeadersLength: reactMaxHeadersLength,
} bootstrapScripts: [bootstrapScript],
) }
)
const getServerInsertedHTML = makeGetServerInsertedHTML({ const getServerInsertedHTML = makeGetServerInsertedHTML({
polyfills, polyfills,
renderServerInsertedHTML, renderServerInsertedHTML,
...@@ -4115,6 +4126,9 @@ async function prerenderToStream( ...@@ -4115,6 +4126,9 @@ async function prerenderToStream(
) )
} }
const { prelude, preludeIsEmpty } =
await processPrelude(unprocessedPrelude)
/** /**
* When prerendering there are three outcomes to consider * When prerendering there are three outcomes to consider
* *
...@@ -4135,6 +4149,9 @@ async function prerenderToStream( ...@@ -4135,6 +4149,9 @@ async function prerenderToStream(
// Dynamic HTML case. // Dynamic HTML case.
metadata.postponed = await getDynamicHTMLPostponedState( metadata.postponed = await getDynamicHTMLPostponedState(
postponed, postponed,
preludeIsEmpty
? DynamicHTMLPreludeState.Empty
: DynamicHTMLPreludeState.Full,
fallbackRouteParams, fallbackRouteParams,
prerenderResumeDataCache prerenderResumeDataCache
) )
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
getDynamicDataPostponedState, getDynamicDataPostponedState,
getDynamicHTMLPostponedState, getDynamicHTMLPostponedState,
parsePostponedState, parsePostponedState,
DynamicHTMLPreludeState,
} from './postponed-state' } from './postponed-state'
describe('getDynamicHTMLPostponedState', () => { describe('getDynamicHTMLPostponedState', () => {
...@@ -30,6 +31,7 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -30,6 +31,7 @@ describe('getDynamicHTMLPostponedState', () => {
const state = await getDynamicHTMLPostponedState( const state = await getDynamicHTMLPostponedState(
{ [key]: key, nested: { [key]: key } }, { [key]: key, nested: { [key]: key } },
DynamicHTMLPreludeState.Full,
fallbackRouteParams, fallbackRouteParams,
prerenderResumeDataCache prerenderResumeDataCache
) )
...@@ -37,12 +39,15 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -37,12 +39,15 @@ describe('getDynamicHTMLPostponedState', () => {
const parsed = parsePostponedState(state, { slug: '123' }) const parsed = parsePostponedState(state, { slug: '123' })
expect(parsed).toMatchInlineSnapshot(` expect(parsed).toMatchInlineSnapshot(`
{ {
"data": { "data": [
"123": "123", 1,
"nested": { {
"123": "123", "123": "123",
"nested": {
"123": "123",
},
}, },
}, ],
"renderResumeDataCache": { "renderResumeDataCache": {
"cache": Map { "cache": Map {
"1" => Promise {}, "1" => Promise {},
...@@ -65,10 +70,11 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -65,10 +70,11 @@ describe('getDynamicHTMLPostponedState', () => {
it('serializes a HTML postponed state without fallback params', async () => { it('serializes a HTML postponed state without fallback params', async () => {
const state = await getDynamicHTMLPostponedState( const state = await getDynamicHTMLPostponedState(
{ key: 'value' }, { key: 'value' },
DynamicHTMLPreludeState.Full,
null, null,
createPrerenderResumeDataCache() createPrerenderResumeDataCache()
) )
expect(state).toMatchInlineSnapshot(`"15:{"key":"value"}null"`) expect(state).toMatchInlineSnapshot(`"19:[1,{"key":"value"}]null"`)
}) })
it('can serialize and deserialize a HTML postponed state with fallback params', async () => { it('can serialize and deserialize a HTML postponed state with fallback params', async () => {
...@@ -76,6 +82,7 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -76,6 +82,7 @@ describe('getDynamicHTMLPostponedState', () => {
const fallbackRouteParams = new Map([['slug', key]]) const fallbackRouteParams = new Map([['slug', key]])
const state = await getDynamicHTMLPostponedState( const state = await getDynamicHTMLPostponedState(
{ [key]: key }, { [key]: key },
DynamicHTMLPreludeState.Full,
fallbackRouteParams, fallbackRouteParams,
createPrerenderResumeDataCache() createPrerenderResumeDataCache()
) )
...@@ -85,7 +92,7 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -85,7 +92,7 @@ describe('getDynamicHTMLPostponedState', () => {
const parsed = parsePostponedState(state, params) const parsed = parsePostponedState(state, params)
expect(parsed).toEqual({ expect(parsed).toEqual({
type: DynamicState.HTML, type: DynamicState.HTML,
data: { [value]: value }, data: [1, { [value]: value }],
renderResumeDataCache: createPrerenderResumeDataCache(), renderResumeDataCache: createPrerenderResumeDataCache(),
}) })
...@@ -105,7 +112,7 @@ describe('getDynamicDataPostponedState', () => { ...@@ -105,7 +112,7 @@ describe('getDynamicDataPostponedState', () => {
describe('parsePostponedState', () => { describe('parsePostponedState', () => {
it('parses a HTML postponed state with fallback params', () => { it('parses a HTML postponed state with fallback params', () => {
const state = `2589:39[["slug","%%drp:slug:e9615126684e5%%"]]{"t":2,"d":{"nextSegmentId":2,"rootFormatContext":{"insertionMode":0,"selectedValue":null,"tagScope":0},"progressiveChunkSize":12800,"resumableState":{"idPrefix":"","nextFormID":0,"streamingFormat":0,"instructions":0,"hasBody":true,"hasHtml":true,"unknownResources":{},"dnsResources":{},"connectResources":{"default":{},"anonymous":{},"credentials":{}},"imageResources":{},"styleResources":{},"scriptResources":{"/_next/static/chunks/webpack-6b2534a6458c6fe5.js":null,"/_next/static/chunks/f5e865f6-5e04edf75402c5e9.js":null,"/_next/static/chunks/9440-26a4cfbb73347735.js":null,"/_next/static/chunks/main-app-315ef55d588dbeeb.js":null,"/_next/static/chunks/8630-8e01a4bea783c651.js":null,"/_next/static/chunks/app/layout-1b900e1a3caf3737.js":null},"moduleUnknownResources":{},"moduleScriptResources":{"/_next/static/chunks/webpack-6b2534a6458c6fe5.js":null}},"replayNodes":[["oR",0,[["Context.Provider",0,[["ServerInsertedHTMLProvider",0,[["Context.Provider",0,[["n7",0,[["nU",0,[["nF",0,[["n9",0,[["Fragment",0,[["Context.Provider",2,[["Context.Provider",0,[["Context.Provider",0,[["Context.Provider",0,[["Context.Provider",0,[["Context.Provider",0,[["nY",0,[["nX",0,[["Fragment","c",[["Fragment",0,[["html",1,[["body",0,[["main",3,[["j",0,[["Fragment",0,[["Context.Provider","validation",[["i",2,[["Fragment",0,[["E",0,[["R",0,[["h",0,[["Fragment",0,[["O",0,[["Fragment",0,[["s",0,[["c",0,[["s",0,[["c",0,[["v",0,[["Context.Provider",0,[["Fragment","c",[["j",1,[["Fragment",0,[["Context.Provider","slug|%%drp:slug:e9615126684e5%%|d",[["i",2,[["Fragment",0,[["E",0,[["R",0,[["h",0,[["Fragment",0,[["O",0,[["Fragment",0,[["s",0,[["Fragment",0,[["s",0,[["c",0,[["v",0,[["Context.Provider",0,[["Fragment","c",[["j",1,[["Fragment",0,[["Context.Provider","__PAGE__",[["i",2,[["Fragment",0,[["E",0,[["R",0,[["h",0,[["Fragment",0,[["O",0,[["Suspense",0,[["s",0,[["Fragment",0,[["s",0,[["c",0,[["v",0,[["Context.Provider",0,[["Fragment","c",[["Fragment",0,[],{"1":1}]],null]],null]],null]],null]],null]],null]],null]],null,["Suspense Fallback",0,[],null],0]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],"replaySlots":null}}null` const state = `2593:39[["slug","%%drp:slug:e9615126684e5%%"]][1,{"t":2,"d":{"nextSegmentId":2,"rootFormatContext":{"insertionMode":0,"selectedValue":null,"tagScope":0},"progressiveChunkSize":12800,"resumableState":{"idPrefix":"","nextFormID":0,"streamingFormat":0,"instructions":0,"hasBody":true,"hasHtml":true,"unknownResources":{},"dnsResources":{},"connectResources":{"default":{},"anonymous":{},"credentials":{}},"imageResources":{},"styleResources":{},"scriptResources":{"/_next/static/chunks/webpack-6b2534a6458c6fe5.js":null,"/_next/static/chunks/f5e865f6-5e04edf75402c5e9.js":null,"/_next/static/chunks/9440-26a4cfbb73347735.js":null,"/_next/static/chunks/main-app-315ef55d588dbeeb.js":null,"/_next/static/chunks/8630-8e01a4bea783c651.js":null,"/_next/static/chunks/app/layout-1b900e1a3caf3737.js":null},"moduleUnknownResources":{},"moduleScriptResources":{"/_next/static/chunks/webpack-6b2534a6458c6fe5.js":null}},"replayNodes":[["oR",0,[["Context.Provider",0,[["ServerInsertedHTMLProvider",0,[["Context.Provider",0,[["n7",0,[["nU",0,[["nF",0,[["n9",0,[["Fragment",0,[["Context.Provider",2,[["Context.Provider",0,[["Context.Provider",0,[["Context.Provider",0,[["Context.Provider",0,[["Context.Provider",0,[["nY",0,[["nX",0,[["Fragment","c",[["Fragment",0,[["html",1,[["body",0,[["main",3,[["j",0,[["Fragment",0,[["Context.Provider","validation",[["i",2,[["Fragment",0,[["E",0,[["R",0,[["h",0,[["Fragment",0,[["O",0,[["Fragment",0,[["s",0,[["c",0,[["s",0,[["c",0,[["v",0,[["Context.Provider",0,[["Fragment","c",[["j",1,[["Fragment",0,[["Context.Provider","slug|%%drp:slug:e9615126684e5%%|d",[["i",2,[["Fragment",0,[["E",0,[["R",0,[["h",0,[["Fragment",0,[["O",0,[["Fragment",0,[["s",0,[["Fragment",0,[["s",0,[["c",0,[["v",0,[["Context.Provider",0,[["Fragment","c",[["j",1,[["Fragment",0,[["Context.Provider","__PAGE__",[["i",2,[["Fragment",0,[["E",0,[["R",0,[["h",0,[["Fragment",0,[["O",0,[["Suspense",0,[["s",0,[["Fragment",0,[["s",0,[["c",0,[["v",0,[["Context.Provider",0,[["Fragment","c",[["Fragment",0,[],{"1":1}]],null]],null]],null]],null]],null]],null]],null]],null,["Suspense Fallback",0,[],null],0]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],null]],"replaySlots":null}}]null`
const params = { const params = {
slug: Math.random().toString(16).slice(3), slug: Math.random().toString(16).slice(3),
} }
......
...@@ -47,7 +47,10 @@ export type DynamicHTMLPostponedState = { ...@@ -47,7 +47,10 @@ export type DynamicHTMLPostponedState = {
/** /**
* The postponed data used by React. * The postponed data used by React.
*/ */
readonly data: object readonly data: [
preludeState: DynamicHTMLPreludeState,
postponed: ReactPostponed,
]
/** /**
* The immutable resume data cache. * The immutable resume data cache.
...@@ -55,27 +58,37 @@ export type DynamicHTMLPostponedState = { ...@@ -55,27 +58,37 @@ export type DynamicHTMLPostponedState = {
readonly renderResumeDataCache: RenderResumeDataCache readonly renderResumeDataCache: RenderResumeDataCache
} }
export const enum DynamicHTMLPreludeState {
Empty = 0,
Full = 1,
}
type ReactPostponed = NonNullable<
import('react-dom/static').PrerenderResult['postponed']
>
export type PostponedState = export type PostponedState =
| DynamicDataPostponedState | DynamicDataPostponedState
| DynamicHTMLPostponedState | DynamicHTMLPostponedState
export async function getDynamicHTMLPostponedState( export async function getDynamicHTMLPostponedState(
data: object, postponed: ReactPostponed,
preludeState: DynamicHTMLPreludeState,
fallbackRouteParams: FallbackRouteParams | null, fallbackRouteParams: FallbackRouteParams | null,
resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache
): Promise<string> { ): Promise<string> {
if (!fallbackRouteParams || fallbackRouteParams.size === 0) { const data: DynamicHTMLPostponedState['data'] = [preludeState, postponed]
const postponedString = JSON.stringify(data) const dataString = JSON.stringify(data)
if (!fallbackRouteParams || fallbackRouteParams.size === 0) {
// Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>` // Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>`
return `${postponedString.length}:${postponedString}${await stringifyResumeDataCache( return `${dataString.length}:${dataString}${await stringifyResumeDataCache(
createRenderResumeDataCache(resumeDataCache) createRenderResumeDataCache(resumeDataCache)
)}` )}`
} }
const replacements: Array<[string, string]> = Array.from(fallbackRouteParams) const replacements: Array<[string, string]> = Array.from(fallbackRouteParams)
const replacementsString = JSON.stringify(replacements) const replacementsString = JSON.stringify(replacements)
const dataString = JSON.stringify(data)
// Serialized as `<replacements.length><replacements><data>` // Serialized as `<replacements.length><replacements><data>`
const postponedString = `${replacementsString.length}${replacementsString}${dataString}` const postponedString = `${replacementsString.length}${replacementsString}${dataString}`
...@@ -168,10 +181,7 @@ export function parsePostponedState( ...@@ -168,10 +181,7 @@ export function parsePostponedState(
} }
} }
export function getPostponedFromState(state: PostponedState): any { export function getPostponedFromState(state: DynamicHTMLPostponedState) {
if (state.type === DynamicState.DATA) { const [preludeState, postponed] = state.data
return null return { preludeState, postponed }
}
return state.data
} }
...@@ -449,32 +449,40 @@ function createDeferredSuffixStream( ...@@ -449,32 +449,40 @@ function createDeferredSuffixStream(
}) })
} }
// Merge two streams into one. Ensure the final transform stream is closed function createFlightDataInjectionTransformStream(
// when both are finished. stream: ReadableStream<Uint8Array>,
function createMergedTransformStream( delayDataUntilFirstHtmlChunk: boolean
stream: ReadableStream<Uint8Array>
): TransformStream<Uint8Array, Uint8Array> { ): TransformStream<Uint8Array, Uint8Array> {
let htmlStreamFinished = false
let pull: Promise<void> | null = null let pull: Promise<void> | null = null
let donePulling = false let donePulling = false
async function startPulling(controller: TransformStreamDefaultController) { function startOrContinuePulling(
if (pull) { controller: TransformStreamDefaultController
return ) {
if (!pull) {
pull = startPulling(controller)
} }
return pull
}
async function startPulling(controller: TransformStreamDefaultController) {
const reader = stream.getReader() const reader = stream.getReader()
// NOTE: streaming flush if (delayDataUntilFirstHtmlChunk) {
// We are buffering here for the inlined data stream because the // NOTE: streaming flush
// "shell" stream might be chunkenized again by the underlying stream // We are buffering here for the inlined data stream because the
// implementation, e.g. with a specific high-water mark. To ensure it's // "shell" stream might be chunkenized again by the underlying stream
// the safe timing to pipe the data stream, this extra tick is // implementation, e.g. with a specific high-water mark. To ensure it's
// necessary. // the safe timing to pipe the data stream, this extra tick is
// necessary.
// We don't start reading until we've left the current Task to ensure
// that it's inserted after flushing the shell. Note that this implementation // We don't start reading until we've left the current Task to ensure
// might get stale if impl details of Fizz change in the future. // that it's inserted after flushing the shell. Note that this implementation
await atLeastOneTask() // might get stale if impl details of Fizz change in the future.
await atLeastOneTask()
}
try { try {
while (true) { while (true) {
...@@ -484,6 +492,12 @@ function createMergedTransformStream( ...@@ -484,6 +492,12 @@ function createMergedTransformStream(
return return
} }
// We want to prioritize HTML over RSC data.
// The SSR render is based on the same RSC stream, so when we get a new RSC chunk,
// we're likely to produce an HTML chunk as well, so give it a chance to flush first.
if (!delayDataUntilFirstHtmlChunk && !htmlStreamFinished) {
await atLeastOneTask()
}
controller.enqueue(value) controller.enqueue(value)
} }
} catch (err) { } catch (err) {
...@@ -492,19 +506,25 @@ function createMergedTransformStream( ...@@ -492,19 +506,25 @@ function createMergedTransformStream(
} }
return new TransformStream({ return new TransformStream({
start(controller) {
if (!delayDataUntilFirstHtmlChunk) {
startOrContinuePulling(controller)
}
},
transform(chunk, controller) { transform(chunk, controller) {
controller.enqueue(chunk) controller.enqueue(chunk)
// Start the streaming if it hasn't already been started yet. // Start the streaming if it hasn't already been started yet.
if (!pull) { if (delayDataUntilFirstHtmlChunk) {
pull = startPulling(controller) startOrContinuePulling(controller)
} }
}, },
flush(controller) { flush(controller) {
htmlStreamFinished = true
if (donePulling) { if (donePulling) {
return return
} }
return pull || startPulling(controller) return startOrContinuePulling(controller)
}, },
}) })
} }
...@@ -711,7 +731,9 @@ export async function continueFizzStream( ...@@ -711,7 +731,9 @@ export async function continueFizzStream(
: null, : null,
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
inlinedDataStream ? createMergedTransformStream(inlinedDataStream) : null, inlinedDataStream
? createFlightDataInjectionTransformStream(inlinedDataStream, true)
: null,
// Validate the root layout for missing html or body tags // Validate the root layout for missing html or body tags
validateRootLayout ? createRootLayoutValidatorStream() : null, validateRootLayout ? createRootLayoutValidatorStream() : null,
...@@ -781,7 +803,9 @@ export async function continueStaticPrerender( ...@@ -781,7 +803,9 @@ export async function continueStaticPrerender(
// Transform metadata // Transform metadata
.pipeThrough(createMetadataTransformStream(getServerInsertedMetadata)) .pipeThrough(createMetadataTransformStream(getServerInsertedMetadata))
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream)) .pipeThrough(
createFlightDataInjectionTransformStream(inlinedDataStream, true)
)
// Close tags should always be deferred to the end // Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream()) .pipeThrough(createMoveSuffixStream())
) )
...@@ -791,11 +815,13 @@ type ContinueResumeOptions = { ...@@ -791,11 +815,13 @@ type ContinueResumeOptions = {
inlinedDataStream: ReadableStream<Uint8Array> inlinedDataStream: ReadableStream<Uint8Array>
getServerInsertedHTML: () => Promise<string> getServerInsertedHTML: () => Promise<string>
getServerInsertedMetadata: () => Promise<string> getServerInsertedMetadata: () => Promise<string>
delayDataUntilFirstHtmlChunk: boolean
} }
export async function continueDynamicHTMLResume( export async function continueDynamicHTMLResume(
renderStream: ReadableStream<Uint8Array>, renderStream: ReadableStream<Uint8Array>,
{ {
delayDataUntilFirstHtmlChunk,
inlinedDataStream, inlinedDataStream,
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
...@@ -810,7 +836,12 @@ export async function continueDynamicHTMLResume( ...@@ -810,7 +836,12 @@ export async function continueDynamicHTMLResume(
// Transform metadata // Transform metadata
.pipeThrough(createMetadataTransformStream(getServerInsertedMetadata)) .pipeThrough(createMetadataTransformStream(getServerInsertedMetadata))
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream)) .pipeThrough(
createFlightDataInjectionTransformStream(
inlinedDataStream,
delayDataUntilFirstHtmlChunk
)
)
// Close tags should always be deferred to the end // Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream()) .pipeThrough(createMoveSuffixStream())
) )
......
'use client'
import { useEffect, useState } from 'react'
export function HydrationIndicator({ id }: { id?: string }) {
const [isMounted, setIsMounted] = useState(false)
useEffect(() => {
setIsMounted(true)
return () => setIsMounted(false)
}, [])
return (
<div id={id} data-is-hydrated={isMounted ? 'true' : 'false'}>
{isMounted ? '🟢 Hydrated' : '⚪ Not hydrated yet'}
</div>
)
}
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { setTimeout } from 'timers/promises'
import { HydrationIndicator } from '../../hydration-indicator'
import type { Metadata } from 'next'
export async function generateMetadata(): Promise<Metadata> {
await connection()
return {
title: 'Resume test',
}
}
export default function Page() {
return (
<main id="shell">
<h1>This is a page with static shell + streaming metadata</h1>
<div>
<p>Static shell</p>
<HydrationIndicator id="shell-hydrated" />
<hr />
<Suspense fallback={<div id="dynamic-fallback">Loading...</div>}>
<SlowServerComponent delay={500} />
</Suspense>
</div>
</main>
)
}
async function SlowServerComponent({ delay }: { delay: number }) {
await connection()
await setTimeout(delay)
const randomValue = Math.floor(Math.random() * 1000)
return (
<div id="dynamic">
<div>{`Random value: ${randomValue}`}</div>
<HydrationIndicator id="dynamic-hydrated" />
</div>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { setTimeout } from 'timers/promises'
import { HydrationIndicator } from '../../hydration-indicator'
export default function Page() {
return (
<main id="shell">
<h1>This is a page with static shell + no streaming metadata</h1>
<div>
<p>Static shell</p>
<HydrationIndicator id="shell-hydrated" />
<hr />
<Suspense fallback={<div id="dynamic-fallback">Loading...</div>}>
<SlowServerComponent delay={500} />
</Suspense>
</div>
</main>
)
}
async function SlowServerComponent({ delay }: { delay: number }) {
await connection()
await setTimeout(delay)
const randomValue = Math.floor(Math.random() * 1000)
return (
<div id="dynamic">
<div>{`Random value: ${randomValue}`}</div>
<HydrationIndicator id="dynamic-hydrated" />
</div>
)
}
import { Suspense } from 'react'
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<Suspense>
<html lang="en">
<body>{children}</body>
</html>
</Suspense>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { setTimeout } from 'timers/promises'
import { HydrationIndicator } from '../../hydration-indicator'
import type { Metadata } from 'next'
export async function generateMetadata(): Promise<Metadata> {
await connection()
return {
title: 'Resume test',
}
}
export default async function Page() {
// Trigger the Suspense-around-body in the root layout so that no static shell is produced
await connection()
return (
<main id="shell">
<h1>This is a page with no static shell + with streaming metadata</h1>
<div>
<p>Dynamic shell</p>
<HydrationIndicator id="shell-hydrated" />
<hr />
<Suspense fallback={<div id="dynamic-fallback">Loading...</div>}>
<SlowServerComponent delay={500} />
</Suspense>
</div>
</main>
)
}
async function SlowServerComponent({ delay }: { delay: number }) {
await connection()
await setTimeout(delay)
const randomValue = Math.floor(Math.random() * 1000)
return (
<div id="dynamic">
<div>{`Random value: ${randomValue}`}</div>
<HydrationIndicator id="dynamic-hydrated" />
</div>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { setTimeout } from 'timers/promises'
import { HydrationIndicator } from '../../hydration-indicator'
export default async function Page() {
// Trigger the Suspense-around-body in the root layout so that no static shell is produced
await connection()
return (
<main id="shell">
<h1>This is a page with no static shell + no streaming metadata</h1>
<div>
<p>Dynamic shell</p>
<HydrationIndicator id="shell-hydrated" />
<hr />
<Suspense fallback={<div id="dynamic-fallback">Loading...</div>}>
<SlowServerComponent delay={500} />
</Suspense>
</div>
</main>
)
}
async function SlowServerComponent({ delay }: { delay: number }) {
await connection()
await setTimeout(delay)
const randomValue = Math.floor(Math.random() * 1000)
return (
<div id="dynamic">
<div>{`Random value: ${randomValue}`}</div>
<HydrationIndicator id="dynamic-hydrated" />
</div>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
ppr: true,
// until the flags are merged, test both `ppr` and `cacheComponents`
// cacheComponents: true,
},
}
export default nextConfig
// CASES:
// shell + no metadata
// shell + streaming metadata
// no shell + no metadata
// no shell + streaming metadata
import { nextTestSetup } from '../../../lib/e2e-utils'
import { retry } from '../../../lib/next-test-utils'
describe('PPR - partial hydration', () => {
const { next, isNextDev } = nextTestSetup({ files: __dirname })
if (isNextDev) {
it.skip('only testable in production', () => {})
return
}
describe.each([
{
description: 'Static shell, no streaming metadata',
path: '/with-shell/without-metadata',
},
{
// Streaming metadata shouldn't affect streaming order, because if we have a static shell,
// then RSC scripts (and thus hydration) don't need to wait for any HTML to be rendered.
// I'm including it here for completeness.
description: 'Static shell, streaming metadata',
path: '/with-shell/with-streaming-metadata',
},
{
description: 'No static shell, no streaming metadata',
path: '/without-shell/without-metadata',
},
{
// Streaming metadata is relevant, because it can affect how the HTML and RSC streams are interleaved.
// If we have no static shell, RSC script tags won't be sent until the first SSR HTML chunk is sent.
// Streaming metadata results in a HTML chunk, and thus it can affect this.
description: 'No static shell, streaming metadata',
path: '/without-shell/with-streaming-metadata',
},
])('$description', ({ path }) => {
it('should hydrate the shell without waiting for slow suspense boundaries', async () => {
const browser = await next.browser(path, {
waitHydration: false,
waitUntil: 'commit', // do not wait for "load", we want to inspect the page as it streams in
})
// Initially, only the shell should be visible
await retry(
async () => {
// The shell should be hydrated as soon as possible,
// without waiting for the dynamic content
expect(
await browser
.elementByCssInstant('#shell-hydrated', { state: 'visible' })
.getAttribute('data-is-hydrated')
).toBe('true')
// The dynamic content hasn't streamed in yet, we should only see the fallback
expect(await browser.elementsByCss('main #dynamic')).toHaveLength(0)
expect(
await browser
.elementByCssInstant('#dynamic-fallback', { state: 'visible' })
.text()
).toContain('Loading...')
},
1000,
50
)
// Then, the slow content should stream in and hydrate
await retry(async () => {
// The shell is already hydrated, this shouldn't change
expect(
await browser
.elementByCssInstant('#shell-hydrated', { state: 'visible' })
.getAttribute('data-is-hydrated')
).toBe('true')
// The dynamic content should be visible and hydrated
expect(
await browser
.elementByCssInstant('#dynamic', { state: 'visible' })
.text()
).toMatch(/Random value: \d+/)
expect(
await browser
.elementByCssInstant('#dynamic-hydrated', { state: 'visible' })
.getAttribute('data-is-hydrated')
).toBe('true')
})
// If the HTML and RSC streams were interleaved correctly, we shouldn't be in quirks mode
// (we would be happen if an RSC script was sent before `<!DOCTYPE html>`)
expect(await browser.eval(() => document.compatMode)).not.toBe(
'BackCompat'
)
})
it('should produce a valid HTML document', async () => {
// This test is meant to check if we're interleaving the HTML and RSC streams correctly.
// In particular, RSC script tags should never appear before the initial HTML
// (which could happen if we e.g. have no static shell and don't wait for it to be rendered before sending them)
const response = await next.fetch(path)
expect(response.status).toBe(200)
const text = await response
.text()
// Ignore the sentinel. For pages with no static shell, it ends up at the front
// and messes up the assertion.
.then((s) => s.replace('<!-- PPR_BOUNDARY_SENTINEL -->', ''))
expect(text).toStartWith('<!DOCTYPE html>')
expect(text).toEndWith('</body></html>')
})
it('should display the shell without JS', async () => {
const browser = await next.browser(path, {
disableJavaScript: true,
waitUntil: 'load', // Unlike the previous test, we want the page to load fully
})
expect(await browser.elementByCss('#shell').text()).toContain(
'This is a page'
)
expect(
await browser
.elementByCss('#shell-hydrated', { state: 'visible' })
.getAttribute('data-is-hydrated')
).toBe('false')
// The dynamic content can't be inserted into the document because we disabled JS,
// so we should only see the fallback
expect(await browser.elementsByCss('main #dynamic')).toHaveLength(0)
expect(
await browser
.elementByCss('#dynamic-fallback', { state: 'visible' })
.text()
).toContain('Loading...')
})
})
})
...@@ -57,6 +57,20 @@ interface ElementHandleExt extends ElementHandle { ...@@ -57,6 +57,20 @@ interface ElementHandleExt extends ElementHandle {
text(): Promise<string> text(): Promise<string>
} }
export type ElementByCssOpts = {
timeout?: number
/** The state of the DOM element. */
state?: 'attached' | 'visible' | 'hidden'
/** The state of the page. */
waitUntil?: false | 'load' | 'domcontentloaded' | 'networkidle'
}
export type PlaywrightNavigationWaitUntil =
| 'load'
| 'domcontentloaded'
| 'networkidle'
| 'commit'
export class Playwright<TCurrent = undefined> { export class Playwright<TCurrent = undefined> {
private activeTrace?: string private activeTrace?: string
private eventCallbacks: Record<EventType, Set<(...args: any[]) => void>> = { private eventCallbacks: Record<EventType, Set<(...args: any[]) => void>> = {
...@@ -229,6 +243,7 @@ export class Playwright<TCurrent = undefined> { ...@@ -229,6 +243,7 @@ export class Playwright<TCurrent = undefined> {
cpuThrottleRate?: number cpuThrottleRate?: number
pushErrorAsConsoleLog?: boolean pushErrorAsConsoleLog?: boolean
beforePageLoad?: (page: Page) => void | Promise<void> beforePageLoad?: (page: Page) => void | Promise<void>
waitUntil?: PlaywrightNavigationWaitUntil
} }
) { ) {
await this.close() await this.close()
...@@ -312,7 +327,7 @@ export class Playwright<TCurrent = undefined> { ...@@ -312,7 +327,7 @@ export class Playwright<TCurrent = undefined> {
await opts?.beforePageLoad?.(page) await opts?.beforePageLoad?.(page)
await page.goto(url, { waitUntil: 'load' }) await page.goto(url, { waitUntil: opts?.waitUntil ?? 'load' })
} }
back(options?: Parameters<Page['goBack']>[0]) { back(options?: Parameters<Page['goBack']>[0]) {
...@@ -371,8 +386,20 @@ export class Playwright<TCurrent = undefined> { ...@@ -371,8 +386,20 @@ export class Playwright<TCurrent = undefined> {
}) })
} }
elementByCss(selector: string) { elementByCss(selector: string, opts?: ElementByCssOpts) {
return this.waitForElementByCss(selector, 5_000) return this.waitForElementByCss(selector, {
timeout: 5_000,
...opts,
})
}
/** A replacement for the default `browser.elementByCss` that doesn't wait for the page to fire "load". */
elementByCssInstant(selector: string, opts?: ElementByCssOpts) {
return this.waitForElementByCss(selector, {
timeout: 10,
waitUntil: false,
...opts,
})
} }
hasElementByCss(selector: string) { hasElementByCss(selector: string) {
...@@ -455,16 +482,29 @@ export class Playwright<TCurrent = undefined> { ...@@ -455,16 +482,29 @@ export class Playwright<TCurrent = undefined> {
) )
} }
waitForElementByCss(selector: string, timeout = 10_000) { waitForElementByCss(selector: string, opts: number | ElementByCssOpts = {}) {
const {
timeout = 10_000,
waitUntil = 'load', // TODO: we should get rid of this and fix the tests that implicitly rely on it
state = 'attached', // TODO: we should probably default to "visible" instead
} = typeof opts === 'number' ? { timeout: opts } : opts
return this.startChain(async () => { return this.startChain(async () => {
const el = await page.waitForSelector(selector, { const el = await page.waitForSelector(selector, {
timeout, timeout,
state: 'attached', state,
}) })
// it seems selenium waits longer and tests rely on this behavior if (waitUntil !== false) {
// so we wait for the load event fire before returning // it seems selenium waits longer and tests rely on this behavior
await page.waitForLoadState() // so we wait for the load event fire before returning
return this.wrapElement(el, selector) await page.waitForLoadState(waitUntil)
}
return this.wrapElement(
// Playwright has `null` as a possible return type in case `state` is `detached`,
// but we don't allow passing that here, so we can assume it's non-null
el!,
selector
)
}) })
} }
......
import { getFullUrl, waitFor } from 'next-test-utils' import { getFullUrl, waitFor } from 'next-test-utils'
import os from 'os' import os from 'os'
import { Playwright } from './browsers/playwright' import {
Playwright,
PlaywrightNavigationWaitUntil,
} from './browsers/playwright'
import { Page } from 'playwright' import { Page } from 'playwright'
export type { Playwright } export type { Playwright }
...@@ -53,6 +56,10 @@ export interface WebdriverOptions { ...@@ -53,6 +56,10 @@ export interface WebdriverOptions {
* allow retrying hydration wait if reload occurs * allow retrying hydration wait if reload occurs
*/ */
retryWaitHydration?: boolean retryWaitHydration?: boolean
/**
* The browser event to wait for during the initial page load. Passed through to `browser.loadPage`
* */
waitUntil?: PlaywrightNavigationWaitUntil
/** /**
* disable cache for page load * disable cache for page load
*/ */
...@@ -114,6 +121,7 @@ export default async function webdriver( ...@@ -114,6 +121,7 @@ export default async function webdriver(
cpuThrottleRate, cpuThrottleRate,
pushErrorAsConsoleLog, pushErrorAsConsoleLog,
userAgent, userAgent,
waitUntil,
} = options } = options
const { Playwright, quit } = await import('./browsers/playwright') const { Playwright, quit } = await import('./browsers/playwright')
...@@ -145,13 +153,14 @@ export default async function webdriver( ...@@ -145,13 +153,14 @@ export default async function webdriver(
cpuThrottleRate, cpuThrottleRate,
beforePageLoad, beforePageLoad,
pushErrorAsConsoleLog, pushErrorAsConsoleLog,
waitUntil,
}) })
console.log(`\n> Loaded browser with ${fullUrl}\n`) console.log(`\n> Loaded browser with ${fullUrl}\n`)
browserTeardown.push(browser.close.bind(browser)) browserTeardown.push(browser.close.bind(browser))
// Wait for application to hydrate // Wait for application to hydrate
if (waitHydration) { if (!disableJavaScript && waitHydration) {
console.log(`\n> Waiting hydration for ${fullUrl}\n`) console.log(`\n> Waiting hydration for ${fullUrl}\n`)
const checkHydrated = async () => { const checkHydrated = async () => {
...@@ -204,7 +213,9 @@ export default async function webdriver( ...@@ -204,7 +213,9 @@ export default async function webdriver(
// This is a temporary workaround for turbopack starting watching too late. // This is a temporary workaround for turbopack starting watching too late.
// So we delay file changes to give it some time // So we delay file changes to give it some time
// to connect the WebSocket and start watching. // to connect the WebSocket and start watching.
if (process.env.IS_TURBOPACK_TEST) { // TODO: Is this still needed? Can we wait in a more useful way, like a socket connection event?
if (process.env.IS_TURBOPACK_TEST && process.env.TURBOPACK_DEV) {
console.log(`\n> Waiting for for turbopack watcher to start`)
await waitFor(1000) await waitFor(1000)
} }
return browser return browser
......
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