Commit cdf79f25 authored by Janka Uryga's avatar Janka Uryga

collect chunks from stages

parent 66c194a5
...@@ -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
const stream = ComponentMod.renderToReadableStream( let 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'],
......
...@@ -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)
}
}
})
}
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