Unverified Commit 94c8017c authored by Josh Story's avatar Josh Story Committed by GitHub

Consolidate HTML render completion cleanup (#99426)

## Summary

End the App Router HTML render span and client-component load tracking
from the same SSR completion callback. This follows #99322’s move to
HTML-render-scoped telemetry and keeps the two measurements from
drifting to different completion points.

Node retains its immediate `onAllReady` boundary; Web SSR handles both
fulfilled and rejected `allReady` outcomes. Prerender and error-recovery
behavior are unchanged.

## Verification

- Existing streamed-render telemetry tests: dev Webpack and production
Turbopack, 5 passed each.
- Production Webpack standalone/Edge suite: 2 passed.
- Existing logger and response-piping unit tests: 11 passed.

<!-- NEXT_JS_LLM -->
parent bc8bf60a
......@@ -2724,16 +2724,6 @@ function initializeClientComponentLoadTracking(
return tracker
}
function finishClientComponentLoadTrackingOnReady(
tracker: ClientComponentLoadTracker | undefined,
allReady: Promise<unknown>
) {
if (tracker) {
const finish = () => tracker.finish()
void allReady.then(finish, finish)
}
}
async function prepareAppPageRender(
req: BaseNextRequest,
res: BaseNextResponse,
......@@ -3789,6 +3779,11 @@ async function renderToStream(
}
)
const finishHtmlRender = () => {
if (renderSpan.isRecording()) renderSpan.end()
tracker?.finish()
}
// Helper to end the span with error status (used when throwing from catch blocks)
const endSpanWithError = (err: unknown) => {
if (!renderSpan.isRecording()) return
......@@ -4248,9 +4243,8 @@ async function renderToStream(
formState
)
// End the span since there's no async rendering in this path
if (renderSpan.isRecording()) renderSpan.end()
tracker?.finish()
// There's no async HTML rendering in this path.
finishHtmlRender()
return chainStreams(
inlinedDataStream,
createDocumentClosingStream()
......@@ -4280,23 +4274,17 @@ async function renderToStream(
tracingMetadata: tracingMetadata,
})
const { stream: htmlStream, allReady } =
await workUnitAsyncStorage.run(
requestStore,
resumeToFizzStream,
resumeAppElement,
postponed,
{
onError: htmlRendererErrorHandler,
onAllReady: tracker ? () => tracker.finish() : undefined,
nonce,
}
)
// End the render span only after React completed rendering (including anything inside Suspense boundaries)
allReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
const { stream: htmlStream } = await workUnitAsyncStorage.run(
requestStore,
resumeToFizzStream,
resumeAppElement,
postponed,
{
onError: htmlRendererErrorHandler,
onAllReady: finishHtmlRender,
nonce,
}
)
return await continueDynamicHTMLResumeNode(htmlStream, {
delayDataUntilFirstHtmlChunk:
......@@ -4337,7 +4325,7 @@ async function renderToStream(
const fizzOptions = {
onError: htmlRendererErrorHandler,
onAllReady: tracker ? () => tracker.finish() : undefined,
onAllReady: finishHtmlRender,
nonce,
onHeaders: (headers: { [header: string]: string }) => {
for (const key in headers) {
......@@ -4362,11 +4350,6 @@ async function renderToStream(
)
)
// End the render span only after React completed rendering (including anything inside Suspense boundaries)
allReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
return await continueFizzStream(htmlStream, {
inlinedDataStream: createNodeInlinedDataStream(
reactServerResult.consume(),
......@@ -4395,9 +4378,8 @@ async function renderToStream(
formState
)
// End the span since there's no async rendering in this path
if (renderSpan.isRecording()) renderSpan.end()
tracker?.finish()
// There's no async HTML rendering in this path.
finishHtmlRender()
return chainStreams(
inlinedDataStream,
createDocumentClosingStream()
......@@ -4436,11 +4418,8 @@ async function renderToStream(
{ onError: htmlRendererErrorHandler, nonce }
)
// End the render span only after React completed rendering (including anything inside Suspense boundaries)
allReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
finishClientComponentLoadTrackingOnReady(tracker, allReady)
// Finish after React renders everything inside Suspense boundaries.
void allReady.then(finishHtmlRender, finishHtmlRender)
return await continueDynamicHTMLResumeWeb(htmlStream, {
delayDataUntilFirstHtmlChunk:
......@@ -4499,11 +4478,8 @@ async function renderToStream(
fizzOptions
)
// End the render span only after React completed rendering (including anything inside Suspense boundaries)
allReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
finishClientComponentLoadTrackingOnReady(tracker, allReady)
// Finish after React renders everything inside Suspense boundaries.
void allReady.then(finishHtmlRender, finishHtmlRender)
return await continueFizzStream(htmlStream, {
inlinedDataStream: createWebInlinedDataStream(
......@@ -4642,30 +4618,25 @@ async function renderToStream(
}
try {
const { stream: errorHtmlStream, allReady: errorAllReady } =
await workUnitAsyncStorage.run(
requestStore,
renderToNodeFizzStream,
<ErrorApp
reactServerStream={errorServerStream}
ServerInsertedHTMLProvider={ServerInsertedHTMLProvider}
preinitScripts={errorPreinitScripts}
nonce={nonce}
images={ctx.renderOpts.images}
/>,
{
nonce,
bootstrapScriptContent: errorBootstrapScriptContent,
bootstrapScripts: [errorBootstrapScript],
formState,
onAllReady: tracker ? () => tracker.finish() : undefined,
},
{ waitForAllReady }
)
errorAllReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
const { stream: errorHtmlStream } = await workUnitAsyncStorage.run(
requestStore,
renderToNodeFizzStream,
<ErrorApp
reactServerStream={errorServerStream}
ServerInsertedHTMLProvider={ServerInsertedHTMLProvider}
preinitScripts={errorPreinitScripts}
nonce={nonce}
images={ctx.renderOpts.images}
/>,
{
nonce,
bootstrapScriptContent: errorBootstrapScriptContent,
bootstrapScripts: [errorBootstrapScript],
formState,
onAllReady: finishHtmlRender,
},
{ waitForAllReady }
)
return await continueFizzStream(errorHtmlStream, {
inlinedDataStream: createNodeInlinedDataStream(
......@@ -4758,10 +4729,8 @@ async function renderToStream(
}
)
errorAllReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
finishClientComponentLoadTrackingOnReady(tracker, errorAllReady)
// Finish after React renders the error document.
void errorAllReady.then(finishHtmlRender, finishHtmlRender)
return await continueFizzStream(errorHtmlStream, {
inlinedDataStream: createWebInlinedDataStream(
......
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