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
cdf79f25
Commit
cdf79f25
authored
Oct 19, 2025
by
Janka Uryga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
collect chunks from stages
parent
66c194a5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
4 deletions
+76
-4
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+33
-4
staged-validation.tsx
packages/next/src/server/app-render/staged-validation.tsx
+43
-0
No files found.
packages/next/src/server/app-render/app-render.tsx
View file @
cdf79f25
...
@@ -212,7 +212,11 @@ import { createPromiseWithResolvers } from '../../shared/lib/promise-with-resolv
...
@@ -212,7 +212,11 @@ import { createPromiseWithResolvers } from '../../shared/lib/promise-with-resolv
import
{
ImageConfigContext
}
from
'../../shared/lib/image-config-context.shared-runtime'
import
{
ImageConfigContext
}
from
'../../shared/lib/image-config-context.shared-runtime'
import
{
imageConfigDefault
}
from
'../../shared/lib/image-config'
import
{
imageConfigDefault
}
from
'../../shared/lib/image-config'
import
{
RenderStage
,
StagedRenderingController
}
from
'./staged-rendering'
import
{
RenderStage
,
StagedRenderingController
}
from
'./staged-rendering'
import
{
anySegmentHasRuntimePrefetchEnabled
}
from
'./staged-validation'
import
{
anySegmentHasRuntimePrefetchEnabled
,
collectStageChunksFromStagedRender
,
type
StageChunks
,
}
from
'./staged-validation'
export
type
GetDynamicParamFromSegment
=
(
export
type
GetDynamicParamFromSegment
=
(
// [slug] / [[slug]] / [...slug]
// [slug] / [[slug]] / [...slug]
...
@@ -2808,8 +2812,9 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -2808,8 +2812,9 @@ async function renderWithRestartOnCacheMissInDev(
// If the render is restarted, we'll recreate a fresh request store
// If the render is restarted, we'll recreate a fresh request store
let
requestStore
:
RequestStore
=
initialRequestStore
let
requestStore
:
RequestStore
=
initialRequestStore
const
getCurrentStage
=
()
=>
requestStore
.
stagedRendering
!
.
currentStage
const
environmentName
=
()
=>
{
const
environmentName
=
()
=>
{
const
currentStage
=
requestStore
.
stagedRendering
!
.
currentStage
const
currentStage
=
getCurrentStage
()
switch
(
currentStage
)
{
switch
(
currentStage
)
{
case
RenderStage
.
Static
:
case
RenderStage
.
Static
:
return
'Prerender'
return
'Prerender'
...
@@ -2823,6 +2828,8 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -2823,6 +2828,8 @@ async function renderWithRestartOnCacheMissInDev(
}
}
}
}
let
stageChunksPromise
:
Promise
<
StageChunks
>
=
null
!
//===============================================
//===============================================
// Initial render
// Initial render
//===============================================
//===============================================
...
@@ -2869,7 +2876,7 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -2869,7 +2876,7 @@ async function renderWithRestartOnCacheMissInDev(
pipelineInSequentialTasks
(
pipelineInSequentialTasks
(
()
=>
{
()
=>
{
// Static stage
// Static stage
cons
t
stream
=
ComponentMod
.
renderToReadableStream
(
le
t
stream
=
ComponentMod
.
renderToReadableStream
(
initialRscPayload
,
initialRscPayload
,
clientReferenceManifest
.
clientModules
,
clientReferenceManifest
.
clientModules
,
{
{
...
@@ -2886,6 +2893,15 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -2886,6 +2893,15 @@ async function renderWithRestartOnCacheMissInDev(
signal
:
initialReactController
.
signal
,
signal
:
initialReactController
.
signal
,
}
}
)
)
let
teedStream
:
typeof
stream
;[
stream
,
teedStream
]
=
stream
.
tee
()
stageChunksPromise
=
collectStageChunksFromStagedRender
(
teedStream
,
getCurrentStage
)
stageChunksPromise
.
catch
(
ignoreReject
)
// 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
// so that it runs after react is finished running its abort code.
// so that it runs after react is finished running its abort code.
...
@@ -2985,7 +3001,7 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -2985,7 +3001,7 @@ async function renderWithRestartOnCacheMissInDev(
pipelineInSequentialTasks
(
pipelineInSequentialTasks
(
()
=>
{
()
=>
{
// Static stage
// Static stage
return
ComponentMod
.
renderToReadableStream
(
let
stream
=
ComponentMod
.
renderToReadableStream
(
finalRscPayload
,
finalRscPayload
,
clientReferenceManifest
.
clientModules
,
clientReferenceManifest
.
clientModules
,
{
{
...
@@ -2995,6 +3011,16 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -2995,6 +3011,16 @@ async function renderWithRestartOnCacheMissInDev(
debugChannel
:
debugChannel
?.
serverSide
,
debugChannel
:
debugChannel
?.
serverSide
,
}
}
)
)
let
teedStream
:
typeof
stream
;[
stream
,
teedStream
]
=
stream
.
tee
()
stageChunksPromise
=
collectStageChunksFromStagedRender
(
teedStream
,
getCurrentStage
)
stageChunksPromise
.
catch
(
ignoreReject
)
return
stream
},
},
(
stream
)
=>
{
(
stream
)
=>
{
// Runtime stage
// Runtime stage
...
@@ -3017,9 +3043,12 @@ async function renderWithRestartOnCacheMissInDev(
...
@@ -3017,9 +3043,12 @@ async function renderWithRestartOnCacheMissInDev(
stream
:
finalServerStream
,
stream
:
finalServerStream
,
debugChannel
,
debugChannel
,
requestStore
,
requestStore
,
stageChunksPromise
,
}
}
}
}
function
ignoreReject
()
{}
function
createAsyncApiPromisesInDev
(
function
createAsyncApiPromisesInDev
(
stagedRendering
:
StagedRenderingController
,
stagedRendering
:
StagedRenderingController
,
cookies
:
RequestStore
[
'cookies'
],
cookies
:
RequestStore
[
'cookies'
],
...
...
packages/next/src/server/app-render/staged-validation.tsx
View file @
cdf79f25
...
@@ -2,6 +2,7 @@ import { getLayoutOrPageModule } from '../lib/app-dir-module'
...
@@ -2,6 +2,7 @@ import { getLayoutOrPageModule } from '../lib/app-dir-module'
import
type
{
LoaderTree
}
from
'../lib/app-dir-module'
import
type
{
LoaderTree
}
from
'../lib/app-dir-module'
import
{
parseLoaderTree
}
from
'../../shared/lib/router/utils/parse-loader-tree'
import
{
parseLoaderTree
}
from
'../../shared/lib/router/utils/parse-loader-tree'
import
type
{
AppSegmentConfig
}
from
'../../build/segment-config/app/app-segment-config'
import
type
{
AppSegmentConfig
}
from
'../../build/segment-config/app/app-segment-config'
import
{
RenderStage
}
from
'./staged-rendering'
export
async
function
anySegmentHasRuntimePrefetchEnabled
(
export
async
function
anySegmentHasRuntimePrefetchEnabled
(
tree
:
LoaderTree
tree
:
LoaderTree
...
@@ -30,3 +31,45 @@ export async function anySegmentHasRuntimePrefetchEnabled(
...
@@ -30,3 +31,45 @@ export async function anySegmentHasRuntimePrefetchEnabled(
return
false
return
false
}
}
export
type
StageChunks
=
Record
<
RenderStage
,
Uint8Array
[]
>
export
function
collectStageChunksFromStagedRender
(
stream
:
ReadableStream
<
Uint8Array
>
,
getCurrentStage
:
()
=>
RenderStage
):
Promise
<
StageChunks
>
{
let
lastChunkStage
:
RenderStage
|
null
=
null
const
chunks
:
StageChunks
=
{
[
RenderStage
.
Static
]:
[],
[
RenderStage
.
Runtime
]:
[],
[
RenderStage
.
Dynamic
]:
[],
}
// Gather the chunks and group them into stages.
return
new
Promise
<
StageChunks
>
(
async
(
resolve
,
reject
)
=>
{
const
reader
=
stream
.
getReader
()
while
(
true
)
{
let
item
:
ReadableStreamReadResult
<
Uint8Array
<
ArrayBufferLike
>>
try
{
item
=
await
reader
.
read
()
}
catch
(
err
)
{
return
reject
(
err
)
}
if
(
!
item
.
done
)
{
const
currentStage
=
getCurrentStage
()
// If we changed to a new stage, we have to copy over the chunks emitted in the previous stage --
// stage N+1 is a superset of stage N.
if
(
lastChunkStage
!==
null
&&
lastChunkStage
!==
currentStage
)
{
chunks
[
currentStage
].
push
(...
chunks
[
lastChunkStage
])
}
chunks
[
currentStage
].
push
(
item
.
value
)
lastChunkStage
=
currentStage
}
else
{
// TODO: we should consider an API that allows yielding a stage's chunks as soon as it completes
return
resolve
(
chunks
)
}
}
})
}
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