@@ -2150,24 +2150,24 @@ async function renderToStream(
2150
2150
return payload
2151
2151
}
2152
2152
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
+ ) => {
2159
2156
const [ readableSsr , readableBrowser ] =
2160
2157
debugChannel . clientSide . readable . tee ( )
2161
2158
2162
2159
reactDebugStream = readableSsr
2163
2160
2164
- setReactDebugChannel (
2161
+ setReactDebugChannel ! (
2165
2162
{ readable : readableBrowser } ,
2166
2163
htmlRequestId ,
2167
2164
requestId
2168
2165
)
2169
2166
}
2170
2167
2168
+ const environmentName = ( ) =>
2169
+ requestStore . prerenderPhase === true ? 'Prerender' : 'Server'
2170
+
2171
2171
// Try to render the page and see if there's any cache misses.
2172
2172
// If there are, wait for caches to finish and restart the render.
2173
2173
@@ -2189,10 +2189,9 @@ async function renderToStream(
2189
2189
requestStore . cacheSignal = cacheSignal
2190
2190
2191
2191
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 ( )
2196
2195
2197
2196
const initialRscPayload = await getPayload ( )
2198
2197
const maybeInitialServerStream = await workUnitAsyncStorage . run (
@@ -2209,7 +2208,7 @@ async function renderToStream(
2209
2208
onError : serverComponentsErrorHandler ,
2210
2209
environmentName,
2211
2210
filterStackFrame,
2212
- debugChannel : bufferedServerDebugChannel ?. channel ,
2211
+ debugChannel : intialRenderDebugChannel ?. serverSide ,
2213
2212
signal : initialRenderReactController . signal ,
2214
2213
}
2215
2214
)
@@ -2239,9 +2238,9 @@ async function renderToStream(
2239
2238
if ( maybeInitialServerStream !== null ) {
2240
2239
// No cache misses. We can use the stream as is.
2241
2240
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 )
2245
2244
}
2246
2245
2247
2246
reactServerResult = new ReactServerResult ( maybeInitialServerStream )
@@ -2280,6 +2279,15 @@ async function renderToStream(
2280
2279
requestStore . prerenderPhase = undefined
2281
2280
requestStore . usedDynamic = undefined
2282
2281
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
+
2283
2291
const finalRscPayload = await getPayload ( )
2284
2292
const finalServerStream = await workUnitAsyncStorage . run (
2285
2293
requestStore ,
@@ -2294,9 +2302,7 @@ async function renderToStream(
2294
2302
onError : serverComponentsErrorHandler ,
2295
2303
environmentName,
2296
2304
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 ,
2300
2306
}
2301
2307
)
2302
2308
} ,
@@ -2723,41 +2729,6 @@ function createDebugChannel(): DebugChannelPair | undefined {
2723
2729
}
2724
2730
}
2725
2731
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
-
2761
2732
function createValidationOutlet ( ) {
2762
2733
let resolveValidation : ( value : React . ReactNode ) => void
2763
2734
let outlet = new Promise < React . ReactNode > ( ( resolve ) => {
0 commit comments