Commit bca423c5 authored by Jimmy Lai's avatar Jimmy Lai

fix: restore app-page stream helper and trim runInContext wrappers

parent 3e1ece70
...@@ -121,14 +121,14 @@ export class ReactServerResult { ...@@ -121,14 +121,14 @@ export class ReactServerResult {
this._stream = tee[0] this._stream = tee[0]
return tee[1] return tee[1]
} else if (process.env.NEXT_RUNTIME !== 'edge') { } else if (process.env.NEXT_RUNTIME !== 'edge') {
return this._runInContext(() => { // Node tee callback handlers run on event boundaries, so keep request ALS
// context stable for whichever branch is consumed later.
const [primary, secondary] = teeNodeReadable( const [primary, secondary] = teeNodeReadable(
this._stream as NodeReadable, this._stream as NodeReadable,
this._runInContext this._runInContext
) )
this._stream = primary this._stream = primary
return secondary return secondary
})
} else { } else {
throw new Error('Cannot tee a Node.js stream in the edge runtime') throw new Error('Cannot tee a Node.js stream in the edge runtime')
} }
......
...@@ -845,24 +845,15 @@ async function stagedRenderToReadableStreamWithoutCachesInDev( ...@@ -845,24 +845,15 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
const { clientModules } = getClientReferenceManifest() const { clientModules } = getClientReferenceManifest()
const rscPayload = await getPayload(requestStore) const rscPayload = await getPayload(requestStore)
const runInContext = <T,>(fn: () => T): T =>
workUnitAsyncStorage.run(requestStore, fn)
return await workUnitAsyncStorage.run( return await workUnitAsyncStorage.run(
requestStore, requestStore,
scheduleInSequentialTasks, scheduleInSequentialTasks,
() => { () => {
stageController.advanceStage(RenderStage.Static) stageController.advanceStage(RenderStage.Static)
return renderToFlightStream( return renderToFlightStream(ctx.componentMod, rscPayload, clientModules, {
ctx.componentMod,
rscPayload,
clientModules,
{
...options, ...options,
environmentName, environmentName,
}, })
runInContext
)
}, },
() => { () => {
stageController.advanceStage(RenderStage.Dynamic) stageController.advanceStage(RenderStage.Dynamic)
...@@ -2847,6 +2838,8 @@ async function renderToStream( ...@@ -2847,6 +2838,8 @@ async function renderToStream(
setReactDebugChannel(browserChannel, htmlRequestId, requestId) setReactDebugChannel(browserChannel, htmlRequestId, requestId)
} }
// Node stream callbacks can run outside the originating ALS scope, so
// preserve the request store explicitly for downstream handlers.
const runInContext = <T,>(fn: () => T): T => const runInContext = <T,>(fn: () => T): T =>
workUnitAsyncStorage.run(requestStore, fn) workUnitAsyncStorage.run(requestStore, fn)
reactServerResult = new ReactServerResult( reactServerResult = new ReactServerResult(
...@@ -3266,6 +3259,8 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -3266,6 +3259,8 @@ async function renderWithRestartOnCacheMissInDev(
initialStageController.advanceStage(RenderStage.Static) initialStageController.advanceStage(RenderStage.Static)
startTime = performance.now() + performance.timeOrigin startTime = performance.now() + performance.timeOrigin
// teeNodeReadable installs event handlers (`data`/`drain`/`end`/`close`)
// that may execute outside the current ALS frame.
const runInContext = <T,>(fn: () => T): T => const runInContext = <T,>(fn: () => T): T =>
workUnitAsyncStorage.run(requestStore, fn) workUnitAsyncStorage.run(requestStore, fn)
...@@ -3280,8 +3275,7 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -3280,8 +3275,7 @@ async function renderWithRestartOnCacheMissInDev(
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
signal: initialReactController.signal, signal: initialReactController.signal,
}, }
runInContext
) )
// If we abort the render, we want to reject the stage-dependent promises as well. // If we abort the render, we want to reject the stage-dependent promises as well.
// Note that we want to install this listener after the render is started // Note that we want to install this listener after the render is started
...@@ -3448,6 +3442,8 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -3448,6 +3442,8 @@ async function renderWithRestartOnCacheMissInDev(
finalStageController.advanceStage(RenderStage.Static) finalStageController.advanceStage(RenderStage.Static)
startTime = performance.now() + performance.timeOrigin startTime = performance.now() + performance.timeOrigin
// teeNodeReadable installs event handlers (`data`/`drain`/`end`/`close`)
// that may execute outside the current ALS frame.
const runInContext = <T,>(fn: () => T): T => const runInContext = <T,>(fn: () => T): T =>
workUnitAsyncStorage.run(requestStore, fn) workUnitAsyncStorage.run(requestStore, fn)
...@@ -3461,8 +3457,7 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -3461,8 +3457,7 @@ async function renderWithRestartOnCacheMissInDev(
startTime, startTime,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
}, }
runInContext
) )
if ( if (
...@@ -5889,6 +5884,8 @@ async function prerenderToStream( ...@@ -5889,6 +5884,8 @@ async function prerenderToStream(
errorType errorType
) )
// Keep prerender-legacy async storage stable across node stream event
// callbacks and pipeable renderer hooks.
const runInLegacyContext = <T,>(fn: () => T): T => const runInLegacyContext = <T,>(fn: () => T): T =>
workUnitAsyncStorage.run(prerenderLegacyStore, fn) workUnitAsyncStorage.run(prerenderLegacyStore, fn)
......
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