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,24 +4274,18 @@ async function renderToStream( ...@@ -4280,24 +4274,18 @@ 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: tracker ? () => tracker.finish() : undefined, onAllReady: finishHtmlRender,
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:
preludeState === DynamicHTMLPreludeState.Empty, preludeState === DynamicHTMLPreludeState.Empty,
...@@ -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,8 +4618,7 @@ async function renderToStream( ...@@ -4642,8 +4618,7 @@ 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
...@@ -4658,15 +4633,11 @@ async function renderToStream( ...@@ -4658,15 +4633,11 @@ async function renderToStream(
bootstrapScriptContent: errorBootstrapScriptContent, bootstrapScriptContent: errorBootstrapScriptContent,
bootstrapScripts: [errorBootstrapScript], bootstrapScripts: [errorBootstrapScript],
formState, formState,
onAllReady: tracker ? () => tracker.finish() : undefined, onAllReady: finishHtmlRender,
}, },
{ waitForAllReady } { waitForAllReady }
) )
errorAllReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
return await continueFizzStream(errorHtmlStream, { return await continueFizzStream(errorHtmlStream, {
inlinedDataStream: createNodeInlinedDataStream( inlinedDataStream: createNodeInlinedDataStream(
// This is intentionally using the readable datastream from the // This is intentionally using the readable datastream from the
...@@ -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