Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
fe044c9f
Commit
fe044c9f
authored
Feb 08, 2026
by
Jimmy Lai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: preserve request async storage in node flight streams
parent
164ccd1c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
34 deletions
+64
-34
app-render-prerender-utils.ts
.../next/src/server/app-render/app-render-prerender-utils.ts
+32
-19
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+25
-12
pipeable-stream-wrappers.ts
...es/next/src/server/app-render/pipeable-stream-wrappers.ts
+7
-3
No files found.
packages/next/src/server/app-render/app-render-prerender-utils.ts
View file @
fe044c9f
...
@@ -99,9 +99,14 @@ export function prerenderAndAbortInSequentialTasksWithStages<R>(
...
@@ -99,9 +99,14 @@ export function prerenderAndAbortInSequentialTasksWithStages<R>(
// in one that doesn't close even when the underlying is complete.
// in one that doesn't close even when the underlying is complete.
export
class
ReactServerResult
{
export
class
ReactServerResult
{
private
_stream
:
null
|
ReadableStream
<
Uint8Array
>
|
NodeReadable
private
_stream
:
null
|
ReadableStream
<
Uint8Array
>
|
NodeReadable
private
readonly
_runInContext
:
<
T
>
(
fn
:
()
=>
T
)
=>
T
constructor
(
stream
:
ReadableStream
<
Uint8Array
>
|
NodeReadable
)
{
constructor
(
stream
:
ReadableStream
<
Uint8Array
>
|
NodeReadable
,
runInContext
?:
<
T
>
(
fn
:
()
=>
T
)
=>
T
)
{
this
.
_stream
=
stream
this
.
_stream
=
stream
this
.
_runInContext
=
runInContext
??
((
fn
)
=>
fn
())
}
}
tee
():
ReadableStream
<
Uint8Array
>
|
NodeReadable
{
tee
():
ReadableStream
<
Uint8Array
>
|
NodeReadable
{
...
@@ -121,25 +126,33 @@ export class ReactServerResult {
...
@@ -121,25 +126,33 @@ export class ReactServerResult {
// buffer fills (e.g. flight data not pulling yet due to
// buffer fills (e.g. flight data not pulling yet due to
// delayDataUntilFirstHtmlChunk), pipe pauses the source, starving
// delayDataUntilFirstHtmlChunk), pipe pauses the source, starving
// the other side (SSR), causing a deadlock.
// the other side (SSR), causing a deadlock.
const
{
PassThrough
}
=
return
this
.
_runInContext
(()
=>
{
require
(
'node:stream'
)
as
typeof
import
(
'node:stream'
)
const
{
PassThrough
}
=
const
pt1
=
new
PassThrough
()
require
(
'node:stream'
)
as
typeof
import
(
'node:stream'
)
const
pt2
=
new
PassThrough
()
const
pt1
=
new
PassThrough
()
const
source
=
this
.
_stream
const
pt2
=
new
PassThrough
()
source
.
on
(
'data'
,
(
chunk
:
Buffer
|
Uint8Array
)
=>
{
const
source
=
this
.
_stream
as
NodeReadable
pt1
.
write
(
chunk
)
source
.
on
(
'data'
,
(
chunk
:
Buffer
|
Uint8Array
)
=>
{
pt2
.
write
(
chunk
)
this
.
_runInContext
(()
=>
{
})
pt1
.
write
(
chunk
)
source
.
on
(
'end'
,
()
=>
{
pt2
.
write
(
chunk
)
pt1
.
end
()
})
pt2
.
end
()
})
})
source
.
on
(
'end'
,
()
=>
{
source
.
on
(
'error'
,
(
err
)
=>
{
this
.
_runInContext
(()
=>
{
if
(
!
pt1
.
destroyed
)
pt1
.
destroy
(
err
)
pt1
.
end
()
if
(
!
pt2
.
destroyed
)
pt2
.
destroy
(
err
)
pt2
.
end
()
})
})
source
.
on
(
'error'
,
(
err
)
=>
{
this
.
_runInContext
(()
=>
{
if
(
!
pt1
.
destroyed
)
pt1
.
destroy
(
err
)
if
(
!
pt2
.
destroyed
)
pt2
.
destroy
(
err
)
})
})
this
.
_stream
=
pt1
return
pt2
})
})
this
.
_stream
=
pt1
return
pt2
}
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'
)
}
}
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
fe044c9f
...
@@ -703,7 +703,7 @@ async function generateDynamicFlightRenderResult(
...
@@ -703,7 +703,7 @@ async function generateDynamicFlightRenderResult(
options
options
)
)
if
(
process
.
env
.
NEXT_RUNTIME
!==
'edge'
)
{
if
(
process
.
env
.
__NEXT_USE_NODE_STREAMS
)
{
const
{
renderToPipeableStream
:
flightRenderToPipeableStream
}
=
const
{
renderToPipeableStream
:
flightRenderToPipeableStream
}
=
ctx
.
componentMod
as
any
ctx
.
componentMod
as
any
if
(
flightRenderToPipeableStream
)
{
if
(
flightRenderToPipeableStream
)
{
...
@@ -723,7 +723,8 @@ async function generateDynamicFlightRenderResult(
...
@@ -723,7 +723,8 @@ async function generateDynamicFlightRenderResult(
temporaryReferences
:
options
?.
temporaryReferences
,
temporaryReferences
:
options
?.
temporaryReferences
,
filterStackFrame
,
filterStackFrame
,
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
return
new
FlightRenderResult
(
return
new
FlightRenderResult
(
...
@@ -829,7 +830,8 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
...
@@ -829,7 +830,8 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
{
{
...
options
,
...
options
,
environmentName
,
environmentName
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
},
},
()
=>
{
()
=>
{
...
@@ -2992,7 +2994,9 @@ async function renderToStream(
...
@@ -2992,7 +2994,9 @@ async function renderToStream(
validationDebugChannelClient
validationDebugChannelClient
)
)
reactServerResult
=
new
ReactServerResult
(
serverStream
)
reactServerResult
=
new
ReactServerResult
(
serverStream
,
(
fn
)
=>
workUnitAsyncStorage
.
run
(
finalRequestStore
,
fn
)
)
requestStore
=
finalRequestStore
requestStore
=
finalRequestStore
debugChannel
=
returnedDebugChannel
debugChannel
=
returnedDebugChannel
}
else
{
}
else
{
...
@@ -3012,7 +3016,9 @@ async function renderToStream(
...
@@ -3012,7 +3016,9 @@ async function renderToStream(
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
}
)
)
reactServerResult
=
new
ReactServerResult
(
serverStream
)
reactServerResult
=
new
ReactServerResult
(
serverStream
,
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
}
}
if
(
debugChannel
&&
setReactDebugChannel
)
{
if
(
debugChannel
&&
setReactDebugChannel
)
{
...
@@ -3073,8 +3079,10 @@ async function renderToStream(
...
@@ -3073,8 +3079,10 @@ async function renderToStream(
filterStackFrame
,
filterStackFrame
,
onError
:
serverComponentsErrorHandler
,
onError
:
serverComponentsErrorHandler
,
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
},
)
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
),
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
}
else
{
}
else
{
reactServerResult
=
new
ReactServerResult
(
reactServerResult
=
new
ReactServerResult
(
...
@@ -3088,7 +3096,8 @@ async function renderToStream(
...
@@ -3088,7 +3096,8 @@ async function renderToStream(
onError
:
serverComponentsErrorHandler
,
onError
:
serverComponentsErrorHandler
,
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
}
)
),
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
}
}
}
else
{
}
else
{
...
@@ -3103,7 +3112,8 @@ async function renderToStream(
...
@@ -3103,7 +3112,8 @@ async function renderToStream(
onError
:
serverComponentsErrorHandler
,
onError
:
serverComponentsErrorHandler
,
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
}
)
),
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
}
}
}
}
...
@@ -3498,7 +3508,8 @@ async function renderToStream(
...
@@ -3498,7 +3508,8 @@ async function renderToStream(
{
{
filterStackFrame
,
filterStackFrame
,
onError
:
serverComponentsErrorHandler
,
onError
:
serverComponentsErrorHandler
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
}
else
{
}
else
{
errorServerStream
=
workUnitAsyncStorage
.
run
(
errorServerStream
=
workUnitAsyncStorage
.
run
(
...
@@ -3801,7 +3812,8 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -3801,7 +3812,8 @@ async function renderWithRestartOnCacheMissInDev(
filterStackFrame
,
filterStackFrame
,
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
signal
:
initialReactController
.
signal
,
signal
:
initialReactController
.
signal
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
initialReactController
.
signal
.
addEventListener
(
'abort'
,
()
=>
{
initialReactController
.
signal
.
addEventListener
(
'abort'
,
()
=>
{
initialDataController
.
abort
(
initialReactController
.
signal
.
reason
)
initialDataController
.
abort
(
initialReactController
.
signal
.
reason
)
...
@@ -4009,7 +4021,8 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -4009,7 +4021,8 @@ async function renderWithRestartOnCacheMissInDev(
startTime
,
startTime
,
filterStackFrame
,
filterStackFrame
,
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
)
// Node stream tee: forward data to two PassThrough streams.
// Node stream tee: forward data to two PassThrough streams.
...
...
packages/next/src/server/app-render/pipeable-stream-wrappers.ts
View file @
fe044c9f
...
@@ -145,7 +145,8 @@ export function renderToFlightPipeableStream(
...
@@ -145,7 +145,8 @@ export function renderToFlightPipeableStream(
)
=>
PipeableStream
,
)
=>
PipeableStream
,
model
:
any
,
model
:
any
,
webpackMap
:
any
,
webpackMap
:
any
,
options
?:
any
options
?:
any
,
runInContext
?:
<
T
>
(
fn
:
()
=>
T
)
=>
T
):
Readable
{
):
Readable
{
const
{
PassThrough
}
=
getNodeStream
()
const
{
PassThrough
}
=
getNodeStream
()
const
passthrough
=
new
PassThrough
()
const
passthrough
=
new
PassThrough
()
...
@@ -162,7 +163,10 @@ export function renderToFlightPipeableStream(
...
@@ -162,7 +163,10 @@ export function renderToFlightPipeableStream(
}
}
}
}
const
{
pipe
}
=
renderToPipeableStreamFn
(
model
,
webpackMap
,
options
)
const
run
=
runInContext
??
((
fn
:
()
=>
any
)
=>
fn
())
pipe
(
passthrough
as
unknown
as
Writable
)
const
{
pipe
}
=
run
(()
=>
renderToPipeableStreamFn
(
model
,
webpackMap
,
options
)
)
run
(()
=>
pipe
(
passthrough
as
unknown
as
Writable
))
return
passthrough
return
passthrough
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment