Registry Mirror
Skip to content

File tree

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,4 +3051,60 @@ describe('ReactFlightDOMBrowser', () => {
30513051
`);
30523052
}
30533053
});
3054+
3055+
it('should resolve a cycle between debug info and the value it produces when using a debug channel', async () => {
3056+
// Same as `should resolve a cycle between debug info and the value it produces`, but using a debug channel.
3057+
3058+
function Inner({style}) {
3059+
return <div style={style} />;
3060+
}
3061+
3062+
function Component({style}) {
3063+
return <Inner style={style} />;
3064+
}
3065+
3066+
const style = {};
3067+
const element = <Component style={style} />;
3068+
style.element = element;
3069+
3070+
let debugReadableStreamController;
3071+
3072+
const debugReadableStream = new ReadableStream({
3073+
start(controller) {
3074+
debugReadableStreamController = controller;
3075+
},
3076+
});
3077+
3078+
const rscStream = await serverAct(() =>
3079+
ReactServerDOMServer.renderToReadableStream(element, webpackMap, {
3080+
debugChannel: {
3081+
writable: new WritableStream({
3082+
write(chunk) {
3083+
debugReadableStreamController.enqueue(chunk);
3084+
},
3085+
close() {
3086+
debugReadableStreamController.close();
3087+
},
3088+
}),
3089+
},
3090+
}),
3091+
);
3092+
3093+
function ClientRoot({response}) {
3094+
return use(response);
3095+
}
3096+
3097+
const response = ReactServerDOMClient.createFromReadableStream(rscStream, {
3098+
debugChannel: {readable: debugReadableStream},
3099+
});
3100+
3101+
const container = document.createElement('div');
3102+
const root = ReactDOMClient.createRoot(container);
3103+
3104+
await act(() => {
3105+
root.render(<ClientRoot response={response} />);
3106+
});
3107+
3108+
expect(container.innerHTML).toBe('<div></div>');
3109+
});
30543110
});

‎packages/react-server/src/ReactFlightServer.js‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4300,14 +4300,21 @@ function emitDebugChunk(
43004300

43014301
const json: string = serializeDebugModel(request, 500, debugInfo);
43024302
if (request.debugDestination !== null) {
4303-
// Outline the actual timing information to the debug channel.
4304-
const outlinedId = request.nextChunkId++;
4305-
const debugRow = outlinedId.toString(16) + ':' + json + '\n';
4306-
request.pendingDebugChunks++;
4307-
request.completedDebugChunks.push(stringToChunk(debugRow));
4308-
const row =
4309-
serializeRowHeader('D', id) + '"$' + outlinedId.toString(16) + '"\n';
4310-
request.completedRegularChunks.push(stringToChunk(row));
4303+
if (json[0] === '"' && json[1] === '$') {
4304+
// This is already an outlined reference so we can just emit it directly,
4305+
// without an unnecessary indirection.
4306+
const row = serializeRowHeader('D', id) + json + '\n';
4307+
request.completedRegularChunks.push(stringToChunk(row));
4308+
} else {
4309+
// Outline the debug information to the debug channel.
4310+
const outlinedId = request.nextChunkId++;
4311+
const debugRow = outlinedId.toString(16) + ':' + json + '\n';
4312+
request.pendingDebugChunks++;
4313+
request.completedDebugChunks.push(stringToChunk(debugRow));
4314+
const row =
4315+
serializeRowHeader('D', id) + '"$' + outlinedId.toString(16) + '"\n';
4316+
request.completedRegularChunks.push(stringToChunk(row));
4317+
}
43114318
} else {
43124319
const row = serializeRowHeader('D', id) + json + '\n';
43134320
request.completedRegularChunks.push(stringToChunk(row));

0 commit comments

Comments
 (0)