Skip to content

Commit 093cc4e

Browse files
committed
clean up debug channel logic
1 parent f62db3a commit 093cc4e

File tree

1 file changed

+24
-53
lines changed

1 file changed

+24
-53
lines changed

packages/next/src/server/app-render/app-render.tsx

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,24 +2150,24 @@ async function renderToStream(
21502150
return payload
21512151
}
21522152

2153-
const environmentName = () =>
2154-
requestStore.prerenderPhase === true ? 'Prerender' : 'Server'
2155-
2156-
const debugChannel = setReactDebugChannel && createDebugChannel()
2157-
2158-
if (debugChannel) {
2153+
const setDebugChannelForClientRender = (
2154+
debugChannel: DebugChannelPair
2155+
) => {
21592156
const [readableSsr, readableBrowser] =
21602157
debugChannel.clientSide.readable.tee()
21612158

21622159
reactDebugStream = readableSsr
21632160

2164-
setReactDebugChannel(
2161+
setReactDebugChannel!(
21652162
{ readable: readableBrowser },
21662163
htmlRequestId,
21672164
requestId
21682165
)
21692166
}
21702167

2168+
const environmentName = () =>
2169+
requestStore.prerenderPhase === true ? 'Prerender' : 'Server'
2170+
21712171
// Try to render the page and see if there's any cache misses.
21722172
// If there are, wait for caches to finish and restart the render.
21732173

@@ -2189,10 +2189,9 @@ async function renderToStream(
21892189
requestStore.cacheSignal = cacheSignal
21902190

21912191
const initialRenderReactController = new AbortController()
2192-
// We don't know if we'll use this render, so buffer debug channel writes until we find out.
2193-
const bufferedServerDebugChannel = debugChannel
2194-
? createBufferedServerDebugChannel()
2195-
: undefined
2192+
2193+
const intialRenderDebugChannel =
2194+
setReactDebugChannel && createDebugChannel()
21962195

21972196
const initialRscPayload = await getPayload()
21982197
const maybeInitialServerStream = await workUnitAsyncStorage.run(
@@ -2209,7 +2208,7 @@ async function renderToStream(
22092208
onError: serverComponentsErrorHandler,
22102209
environmentName,
22112210
filterStackFrame,
2212-
debugChannel: bufferedServerDebugChannel?.channel,
2211+
debugChannel: intialRenderDebugChannel?.serverSide,
22132212
signal: initialRenderReactController.signal,
22142213
}
22152214
)
@@ -2239,9 +2238,9 @@ async function renderToStream(
22392238
if (maybeInitialServerStream !== null) {
22402239
// No cache misses. We can use the stream as is.
22412240

2242-
// Since we're using this render, the debug info we've buffered should be written to the real debug channel.
2243-
if (debugChannel && bufferedServerDebugChannel) {
2244-
void bufferedServerDebugChannel.pipeToChannel(debugChannel.serverSide)
2241+
// We're using this render, so we should pass its debug channel to the client render.
2242+
if (intialRenderDebugChannel) {
2243+
setDebugChannelForClientRender(intialRenderDebugChannel)
22452244
}
22462245

22472246
reactServerResult = new ReactServerResult(maybeInitialServerStream)
@@ -2280,6 +2279,15 @@ async function renderToStream(
22802279
requestStore.prerenderPhase = undefined
22812280
requestStore.usedDynamic = undefined
22822281

2282+
// The initial render already wrote to its debug channel. We're not using it,
2283+
// so we need to create a new one.
2284+
const finalRenderDebugChannel =
2285+
setReactDebugChannel && createDebugChannel()
2286+
// We know that we won't discard this render, so we can set the debug channel up immediately.
2287+
if (finalRenderDebugChannel) {
2288+
setDebugChannelForClientRender(finalRenderDebugChannel)
2289+
}
2290+
22832291
const finalRscPayload = await getPayload()
22842292
const finalServerStream = await workUnitAsyncStorage.run(
22852293
requestStore,
@@ -2294,9 +2302,7 @@ async function renderToStream(
22942302
onError: serverComponentsErrorHandler,
22952303
environmentName,
22962304
filterStackFrame,
2297-
// We know we'll use this render, so unlike the initial one,
2298-
// it can write into the debug channel directly instead of buffering.
2299-
debugChannel: debugChannel?.serverSide,
2305+
debugChannel: finalRenderDebugChannel?.serverSide,
23002306
}
23012307
)
23022308
},
@@ -2723,41 +2729,6 @@ function createDebugChannel(): DebugChannelPair | undefined {
27232729
}
27242730
}
27252731

2726-
function createBufferedServerDebugChannel() {
2727-
// We buffer all chunks until we're connected to a real debug channel using `connect()`.
2728-
const chunks: Uint8Array[] = []
2729-
let onWrite = function bufferChunk(chunk: Uint8Array) {
2730-
chunks.push(chunk)
2731-
}
2732-
let onClose: (() => Promise<void>) | undefined = undefined
2733-
2734-
const writable = new WritableStream<Uint8Array>({
2735-
write(chunk) {
2736-
onWrite(chunk)
2737-
},
2738-
close() {
2739-
return onClose?.()
2740-
},
2741-
})
2742-
2743-
return {
2744-
channel: { writable } as DebugChannelServer,
2745-
/** Attach this stream to a real debug channel. */
2746-
async pipeToChannel(debugChannel: DebugChannelServer) {
2747-
// Once we're comitted to using this stream, write out the chunks we already have.
2748-
const writer = debugChannel.writable.getWriter()
2749-
for (const chunk of chunks) {
2750-
await writer.write(chunk)
2751-
}
2752-
chunks.length = 0
2753-
2754-
// After this point, we stop buffering, and future chunks will be written directly to the destination.
2755-
onWrite = writer.write.bind(writer)
2756-
onClose = writer.close.bind(writer)
2757-
},
2758-
}
2759-
}
2760-
27612732
function createValidationOutlet() {
27622733
let resolveValidation: (value: React.ReactNode) => void
27632734
let outlet = new Promise<React.ReactNode>((resolve) => {

0 commit comments

Comments
 (0)