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