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>(
// in one that doesn't close even when the underlying is complete.
export
class
ReactServerResult
{
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
.
_runInContext
=
runInContext
??
((
fn
)
=>
fn
())
}
tee
():
ReadableStream
<
Uint8Array
>
|
NodeReadable
{
...
...
@@ -121,25 +126,33 @@ export class ReactServerResult {
// buffer fills (e.g. flight data not pulling yet due to
// delayDataUntilFirstHtmlChunk), pipe pauses the source, starving
// the other side (SSR), causing a deadlock.
const
{
PassThrough
}
=
require
(
'node:stream'
)
as
typeof
import
(
'node:stream'
)
const
pt1
=
new
PassThrough
()
const
pt2
=
new
PassThrough
()
const
source
=
this
.
_stream
source
.
on
(
'data'
,
(
chunk
:
Buffer
|
Uint8Array
)
=>
{
pt1
.
write
(
chunk
)
pt2
.
write
(
chunk
)
})
source
.
on
(
'end'
,
()
=>
{
pt1
.
end
()
pt2
.
end
()
})
source
.
on
(
'error'
,
(
err
)
=>
{
if
(
!
pt1
.
destroyed
)
pt1
.
destroy
(
err
)
if
(
!
pt2
.
destroyed
)
pt2
.
destroy
(
err
)
return
this
.
_runInContext
(()
=>
{
const
{
PassThrough
}
=
require
(
'node:stream'
)
as
typeof
import
(
'node:stream'
)
const
pt1
=
new
PassThrough
()
const
pt2
=
new
PassThrough
()
const
source
=
this
.
_stream
as
NodeReadable
source
.
on
(
'data'
,
(
chunk
:
Buffer
|
Uint8Array
)
=>
{
this
.
_runInContext
(()
=>
{
pt1
.
write
(
chunk
)
pt2
.
write
(
chunk
)
})
})
source
.
on
(
'end'
,
()
=>
{
this
.
_runInContext
(()
=>
{
pt1
.
end
()
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
{
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(
options
)
if
(
process
.
env
.
NEXT_RUNTIME
!==
'edge'
)
{
if
(
process
.
env
.
__NEXT_USE_NODE_STREAMS
)
{
const
{
renderToPipeableStream
:
flightRenderToPipeableStream
}
=
ctx
.
componentMod
as
any
if
(
flightRenderToPipeableStream
)
{
...
...
@@ -723,7 +723,8 @@ async function generateDynamicFlightRenderResult(
temporaryReferences
:
options
?.
temporaryReferences
,
filterStackFrame
,
debugChannel
:
debugChannel
?.
serverSide
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
return
new
FlightRenderResult
(
...
...
@@ -829,7 +830,8 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
{
...
options
,
environmentName
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
},
()
=>
{
...
...
@@ -2992,7 +2994,9 @@ async function renderToStream(
validationDebugChannelClient
)
reactServerResult
=
new
ReactServerResult
(
serverStream
)
reactServerResult
=
new
ReactServerResult
(
serverStream
,
(
fn
)
=>
workUnitAsyncStorage
.
run
(
finalRequestStore
,
fn
)
)
requestStore
=
finalRequestStore
debugChannel
=
returnedDebugChannel
}
else
{
...
...
@@ -3012,7 +3016,9 @@ async function renderToStream(
debugChannel
:
debugChannel
?.
serverSide
,
}
)
reactServerResult
=
new
ReactServerResult
(
serverStream
)
reactServerResult
=
new
ReactServerResult
(
serverStream
,
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
}
if
(
debugChannel
&&
setReactDebugChannel
)
{
...
...
@@ -3073,8 +3079,10 @@ async function renderToStream(
filterStackFrame
,
onError
:
serverComponentsErrorHandler
,
debugChannel
:
debugChannel
?.
serverSide
,
}
)
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
),
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
}
else
{
reactServerResult
=
new
ReactServerResult
(
...
...
@@ -3088,7 +3096,8 @@ async function renderToStream(
onError
:
serverComponentsErrorHandler
,
debugChannel
:
debugChannel
?.
serverSide
,
}
)
),
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
}
}
else
{
...
...
@@ -3103,7 +3112,8 @@ async function renderToStream(
onError
:
serverComponentsErrorHandler
,
debugChannel
:
debugChannel
?.
serverSide
,
}
)
),
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
}
}
...
...
@@ -3498,7 +3508,8 @@ async function renderToStream(
{
filterStackFrame
,
onError
:
serverComponentsErrorHandler
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
}
else
{
errorServerStream
=
workUnitAsyncStorage
.
run
(
...
...
@@ -3801,7 +3812,8 @@ async function renderWithRestartOnCacheMissInDev(
filterStackFrame
,
debugChannel
:
debugChannel
?.
serverSide
,
signal
:
initialReactController
.
signal
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
initialReactController
.
signal
.
addEventListener
(
'abort'
,
()
=>
{
initialDataController
.
abort
(
initialReactController
.
signal
.
reason
)
...
...
@@ -4009,7 +4021,8 @@ async function renderWithRestartOnCacheMissInDev(
startTime
,
filterStackFrame
,
debugChannel
:
debugChannel
?.
serverSide
,
}
},
(
fn
)
=>
workUnitAsyncStorage
.
run
(
requestStore
,
fn
)
)
// 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(
)
=>
PipeableStream
,
model
:
any
,
webpackMap
:
any
,
options
?:
any
options
?:
any
,
runInContext
?:
<
T
>
(
fn
:
()
=>
T
)
=>
T
):
Readable
{
const
{
PassThrough
}
=
getNodeStream
()
const
passthrough
=
new
PassThrough
()
...
...
@@ -162,7 +163,10 @@ export function renderToFlightPipeableStream(
}
}
const
{
pipe
}
=
renderToPipeableStreamFn
(
model
,
webpackMap
,
options
)
pipe
(
passthrough
as
unknown
as
Writable
)
const
run
=
runInContext
??
((
fn
:
()
=>
any
)
=>
fn
())
const
{
pipe
}
=
run
(()
=>
renderToPipeableStreamFn
(
model
,
webpackMap
,
options
)
)
run
(()
=>
pipe
(
passthrough
as
unknown
as
Writable
))
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