Commit 33cae0fe authored by Jimmy Lai's avatar Jimmy Lai

app-render: wait for request debug stream before resolving Flight

When node streams and the experimental React debug channel are both enabled, request-time Flight decoding can resolve before the debug stream has completed. In restart-on-cache-miss flows this races metadata insertion and can leave metadata tags (title/canonical) in the body fallback path instead of in head, causing use-cache metadata resume tests to time out waiting on head selectors.\n\nTrack debug stream completion in getFlightStream() and gate resolution for workUnit type 'request' until both createFromNodeStream and the debug stream are done.\n\nScope is intentionally narrow: only applies when a debug stream exists, and only for request work units. Other work unit behavior is unchanged.\n\nValidated with:\nHEADLESS=true NEXT_SKIP_ISOLATE=1 NEXT_TEST_MODE=dev __NEXT_USE_NODE_STREAMS=true __NEXT_CACHE_COMPONENTS=true __NEXT_EXPERIMENTAL_DEBUG_CHANNEL=true node_modules/.bin/jest test/e2e/app-dir/use-cache/use-cache.test.ts -t "can resume a cached generateMetadata function|can serialize parent metadata as generateMetadata argument"\n\nAlso verified the same target passes without __NEXT_EXPERIMENTAL_DEBUG_CHANNEL.
parent bed0fcda
...@@ -45,6 +45,7 @@ export function getFlightStream<T>( ...@@ -45,6 +45,7 @@ export function getFlightStream<T>(
getClientReferenceManifest() getClientReferenceManifest()
let newResponse: Promise<T> let newResponse: Promise<T>
let waitForDebugEnd: Promise<void> | undefined
if (flightStream instanceof ReadableStream) { if (flightStream instanceof ReadableStream) {
// The types of flightStream and debugStream should match. // The types of flightStream and debugStream should match.
if (debugStream && !(debugStream instanceof ReadableStream)) { if (debugStream && !(debugStream instanceof ReadableStream)) {
...@@ -87,6 +88,16 @@ export function getFlightStream<T>( ...@@ -87,6 +88,16 @@ export function getFlightStream<T>(
// have a minor type mismatch (Promise<void> vs Promise<undefined>). // have a minor type mismatch (Promise<void> vs Promise<undefined>).
nodeDebugStream = Readable.fromWeb(debugStream as any) nodeDebugStream = Readable.fromWeb(debugStream as any)
} }
// With createFromNodeStream + debugChannel, the response can resolve
// before debug stream completion. In restart-on-cache-miss flows this
// can leave metadata in the body instead of the head for the request
// render. Wait for debug completion for request work units.
waitForDebugEnd = new Promise<void>((resolve, reject) => {
nodeDebugStream!.once('end', resolve)
nodeDebugStream!.once('close', resolve)
nodeDebugStream!.once('error', reject)
})
} }
// react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly // react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly
...@@ -134,6 +145,12 @@ export function getFlightStream<T>( ...@@ -134,6 +145,12 @@ export function getFlightStream<T>(
case 'prerender-ppr': case 'prerender-ppr':
case 'prerender-legacy': case 'prerender-legacy':
case 'request': case 'request':
if (waitForDebugEnd) {
newResponse = Promise.all([newResponse, waitForDebugEnd]).then(
([flightResponse]) => flightResponse
)
}
break
case 'cache': case 'cache':
case 'private-cache': case 'private-cache':
case 'unstable-cache': case 'unstable-cache':
......
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