Use React's debug channel to transport debug info via WebSocket connection (#82748)
In dev mode, React is sending debug info (owners, stacks, console logs
etc.) from the server to the client so that they can be displayed in dev
tools. By default, this info is sent as part of the main RSC payload
(inlined into the HTML, or fetched after interaction). With this PR,
we're now transporting the debug info in a separate debug channel. This
channel currently consists of a unidirectional stream (writable on the
server, readable in the client) that is serialized as binary WebSocket
messages, which are sent through the existing WebSocket connection that
Next.js uses for HMR messages etc.
In a follow up, we will also implement the return channel (writable in
the client, readable on the server). This allows the client to retrieve
debug info lazily and on demand during inspection/introspection.
One benefit of this approach is, that it will allow us to change our
dynamic validation strategy. We should then be able to tee and cut off
the dynamic RSC stream at different points in time (representing the
static prerender, runtime prerender, and dynamic render stages), while
still letting the complete debug info stream in through the separate
channel. This allows us to show the same dynamic validation error
details as before, while reducing the number of renders needed to
accomplish that.
For dogfooding purposes, the feature can be disabled by setting
`experimental.reactDebugChannel` to `false`, in case of unexpected
regressions in dev mode.
### An implementation note on the request ID handling
On the server, at the beginning of the rendering process, we determine a
`requestId` and an `htmlRequestId`.
1. The `requestId` is a UUID that's newly generated for each incoming
request.
2. The `htmlRequestId` is one of two things:
1. For the HTML request, it's the same as the `requestId`.
2. For subsequent RSC requests (for navigations or server actions), it's
the ID of the HTML document from where the request originates. It is
sent as the `x-nextjs-html-request-id` request header.
The HTML request ID is used to identify connected WebSocket clients.
It's sent to the browser as the global `__next_r` variable in the script
tag that also includes the initial RSC instructions. The browser sends
it back to the server as `id` query param of the WebSocket request. On
the server, we store the clients by HTML request ID. When debug chunks
are sent to the browser, we use the HTML request ID to look up the
correct client. (There's only one client per browser tab/window.)
In the browser, we create a debug channel for each request, and we store
them keyed by request ID. For the HTML request, the request ID from
`__next_r` is used as key. For the RSC requests, the `requestId` is
received as `x-nextjs-request-id` response header, and used as key. The
server sends the current request ID as part of the WebSocket message to
the browser, so that the chunks can be enqueued into the correct debug
channel.
closes NAR-310
Showing
This diff is collapsed.
This diff is collapsed.
Please
register
or
sign in
to comment