Unverified Commit 316c0629 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[dev] Define request ID for RSC requests on the client (#84605)

Previously, we defined a request ID for RSC requests on the server. The
request ID is used to tag debug information sent via WebSocket to the
client, which then routes those chunks to the debug channel associated
with this ID.

However, this meant that the client had to await the server response to
retrieve the request ID from the response headers, before we could pass
the response (and the debug channel) to React.

For #84580, we want to pass the response promise directly to React, to
accurately show timing information about the request in the React
DevTools. To achieve this, we now define the request ID on the client,
and send it to the server via a request header. This means that the
client can create the debug channel immediately, without waiting for the
server response.

closes NAR-426
parent b5eba8ff
...@@ -851,5 +851,6 @@ ...@@ -851,5 +851,6 @@
"850": "metadataBase is not a valid URL: %s", "850": "metadataBase is not a valid URL: %s",
"851": "Pass either `webpack` or `turbopack`, not both.", "851": "Pass either `webpack` or `turbopack`, not both.",
"852": "Only custom servers can pass `webpack`, `turbo`, or `turbopack`.", "852": "Only custom servers can pass `webpack`, `turbo`, or `turbopack`.",
"853": "Turbopack build failed" "853": "Turbopack build failed",
"854": "Expected a %s request header."
} }
...@@ -21,6 +21,7 @@ import { ...@@ -21,6 +21,7 @@ import {
NEXT_DID_POSTPONE_HEADER, NEXT_DID_POSTPONE_HEADER,
NEXT_ROUTER_STALE_TIME_HEADER, NEXT_ROUTER_STALE_TIME_HEADER,
NEXT_HTML_REQUEST_ID_HEADER, NEXT_HTML_REQUEST_ID_HEADER,
NEXT_REQUEST_ID_HEADER,
} from '../app-router-headers' } from '../app-router-headers'
import { callServer } from '../../app-call-server' import { callServer } from '../../app-call-server'
import { findSourceMapURL } from '../../app-find-source-map-url' import { findSourceMapURL } from '../../app-find-source-map-url'
...@@ -77,6 +78,7 @@ export type RequestHeaders = { ...@@ -77,6 +78,7 @@ export type RequestHeaders = {
// A header that is only added in test mode to assert on fetch priority // A header that is only added in test mode to assert on fetch priority
'Next-Test-Fetch-Priority'?: RequestInit['priority'] 'Next-Test-Fetch-Priority'?: RequestInit['priority']
[NEXT_HTML_REQUEST_ID_HEADER]?: string // dev-only [NEXT_HTML_REQUEST_ID_HEADER]?: string // dev-only
[NEXT_REQUEST_ID_HEADER]?: string // dev-only
} }
function doMpaNavigation(url: string): FetchServerResponseResult { function doMpaNavigation(url: string): FetchServerResponseResult {
...@@ -230,7 +232,7 @@ export async function fetchServerResponse( ...@@ -230,7 +232,7 @@ export async function fetchServerResponse(
: res.body : res.body
const response = await (createFromNextReadableStream( const response = await (createFromNextReadableStream(
flightStream, flightStream,
res.headers headers
) as Promise<NavigationFlightResponse>) ) as Promise<NavigationFlightResponse>)
if (getAppBuildId() !== response.b) { if (getAppBuildId() !== response.b) {
...@@ -299,8 +301,17 @@ export async function createFetch( ...@@ -299,8 +301,17 @@ export async function createFetch(
headers['x-deployment-id'] = process.env.NEXT_DEPLOYMENT_ID headers['x-deployment-id'] = process.env.NEXT_DEPLOYMENT_ID
} }
if (process.env.NODE_ENV !== 'production' && self.__next_r) { if (process.env.NODE_ENV !== 'production') {
headers[NEXT_HTML_REQUEST_ID_HEADER] = self.__next_r if (self.__next_r) {
headers[NEXT_HTML_REQUEST_ID_HEADER] = self.__next_r
}
// Create a new request ID for the server action request. The server uses
// this to tag debug information sent via WebSocket to the client, which
// then routes those chunks to the debug channel associated with this ID.
headers[NEXT_REQUEST_ID_HEADER] = crypto
.getRandomValues(new Uint32Array(1))[0]
.toString(16)
} }
const fetchOptions: RequestInit = { const fetchOptions: RequestInit = {
...@@ -404,12 +415,12 @@ export async function createFetch( ...@@ -404,12 +415,12 @@ export async function createFetch(
export function createFromNextReadableStream( export function createFromNextReadableStream(
flightStream: ReadableStream<Uint8Array>, flightStream: ReadableStream<Uint8Array>,
responseHeaders: Headers requestHeaders: RequestHeaders
): Promise<unknown> { ): Promise<unknown> {
return createFromReadableStream(flightStream, { return createFromReadableStream(flightStream, {
callServer, callServer,
findSourceMapURL, findSourceMapURL,
debugChannel: createDebugChannel && createDebugChannel(responseHeaders), debugChannel: createDebugChannel && createDebugChannel(requestHeaders),
}) })
} }
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
NEXT_ROUTER_STATE_TREE_HEADER, NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_URL, NEXT_URL,
RSC_CONTENT_TYPE_HEADER, RSC_CONTENT_TYPE_HEADER,
NEXT_REQUEST_ID_HEADER,
} from '../../app-router-headers' } from '../../app-router-headers'
import { UnrecognizedActionError } from '../../unrecognized-action-error' import { UnrecognizedActionError } from '../../unrecognized-action-error'
...@@ -119,8 +120,17 @@ async function fetchServerAction( ...@@ -119,8 +120,17 @@ async function fetchServerAction(
headers[NEXT_URL] = nextUrl headers[NEXT_URL] = nextUrl
} }
if (process.env.NODE_ENV !== 'production' && self.__next_r) { if (process.env.NODE_ENV !== 'production') {
headers[NEXT_HTML_REQUEST_ID_HEADER] = self.__next_r if (self.__next_r) {
headers[NEXT_HTML_REQUEST_ID_HEADER] = self.__next_r
}
// Create a new request ID for the server action request. The server uses
// this to tag debug information sent via WebSocket to the client, which
// then routes those chunks to the debug channel associated with this ID.
headers[NEXT_REQUEST_ID_HEADER] = crypto
.getRandomValues(new Uint32Array(1))[0]
.toString(16)
} }
const res = await fetch(state.canonicalUrl, { method: 'POST', headers, body }) const res = await fetch(state.canonicalUrl, { method: 'POST', headers, body })
...@@ -198,7 +208,7 @@ async function fetchServerAction( ...@@ -198,7 +208,7 @@ async function fetchServerAction(
callServer, callServer,
findSourceMapURL, findSourceMapURL,
temporaryReferences, temporaryReferences,
debugChannel: createDebugChannel && createDebugChannel(res.headers), debugChannel: createDebugChannel && createDebugChannel(headers),
} }
) )
......
...@@ -1432,7 +1432,7 @@ export async function fetchRouteOnCacheMiss( ...@@ -1432,7 +1432,7 @@ export async function fetchRouteOnCacheMiss(
) )
const serverData = await (createFromNextReadableStream( const serverData = await (createFromNextReadableStream(
prefetchStream, prefetchStream,
response.headers headers
) as Promise<RootTreePrefetch>) ) as Promise<RootTreePrefetch>)
if (serverData.buildId !== getAppBuildId()) { if (serverData.buildId !== getAppBuildId()) {
// The server build does not match the client. Treat as a 404. During // The server build does not match the client. Treat as a 404. During
...@@ -1484,7 +1484,7 @@ export async function fetchRouteOnCacheMiss( ...@@ -1484,7 +1484,7 @@ export async function fetchRouteOnCacheMiss(
) )
const serverData = await (createFromNextReadableStream( const serverData = await (createFromNextReadableStream(
prefetchStream, prefetchStream,
response.headers headers
) as Promise<NavigationFlightResponse>) ) as Promise<NavigationFlightResponse>)
if (serverData.b !== getAppBuildId()) { if (serverData.b !== getAppBuildId()) {
// The server build does not match the client. Treat as a 404. During // The server build does not match the client. Treat as a 404. During
...@@ -1630,7 +1630,7 @@ export async function fetchSegmentOnCacheMiss( ...@@ -1630,7 +1630,7 @@ export async function fetchSegmentOnCacheMiss(
) )
const serverData = await (createFromNextReadableStream( const serverData = await (createFromNextReadableStream(
prefetchStream, prefetchStream,
response.headers headers
) as Promise<SegmentPrefetch>) ) as Promise<SegmentPrefetch>)
if (serverData.buildId !== getAppBuildId()) { if (serverData.buildId !== getAppBuildId()) {
// The server build does not match the client. Treat as a 404. During // The server build does not match the client. Treat as a 404. During
...@@ -1750,7 +1750,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -1750,7 +1750,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
) )
const serverData = await (createFromNextReadableStream( const serverData = await (createFromNextReadableStream(
prefetchStream, prefetchStream,
response.headers headers
) as Promise<NavigationFlightResponse>) ) as Promise<NavigationFlightResponse>)
const isResponsePartial = const isResponsePartial =
......
...@@ -23,18 +23,20 @@ export function getOrCreateDebugChannelReadableWriterPair( ...@@ -23,18 +23,20 @@ export function getOrCreateDebugChannelReadableWriterPair(
return pair return pair
} }
export function createDebugChannel(responseHeaders: Headers | undefined): { export function createDebugChannel(
requestHeaders: Record<string, string> | undefined
): {
writable?: WritableStream writable?: WritableStream
readable?: ReadableStream readable?: ReadableStream
} { } {
let requestId: string | undefined let requestId: string | undefined
if (responseHeaders) { if (requestHeaders) {
requestId = responseHeaders.get(NEXT_REQUEST_ID_HEADER) ?? undefined requestId = requestHeaders[NEXT_REQUEST_ID_HEADER] ?? undefined
if (!requestId) { if (!requestId) {
throw new InvariantError( throw new InvariantError(
`Expected a ${JSON.stringify(NEXT_REQUEST_ID_HEADER)} response header.` `Expected a ${JSON.stringify(NEXT_REQUEST_ID_HEADER)} request header.`
) )
} }
} else { } else {
......
...@@ -287,6 +287,7 @@ interface ParsedRequestHeaders { ...@@ -287,6 +287,7 @@ interface ParsedRequestHeaders {
readonly isRSCRequest: boolean readonly isRSCRequest: boolean
readonly nonce: string | undefined readonly nonce: string | undefined
readonly previouslyRevalidatedTags: string[] readonly previouslyRevalidatedTags: string[]
readonly requestId: string | undefined
readonly htmlRequestId: string | undefined readonly htmlRequestId: string | undefined
} }
...@@ -332,10 +333,25 @@ function parseRequestHeaders( ...@@ -332,10 +333,25 @@ function parseRequestHeaders(
options.previewModeId options.previewModeId
) )
const htmlRequestId = let requestId: string | undefined
typeof headers[NEXT_HTML_REQUEST_ID_HEADER] === 'string' let htmlRequestId: string | undefined
? headers[NEXT_HTML_REQUEST_ID_HEADER]
: undefined if (process.env.NODE_ENV !== 'production') {
// The request IDs are only used in development mode to send debug
// information to the matching client (identified by the HTML request ID
// that was sent to the client with the HTML document) for the current
// request (identified by the request ID, as defined by the client).
requestId =
typeof headers[NEXT_REQUEST_ID_HEADER] === 'string'
? headers[NEXT_REQUEST_ID_HEADER]
: undefined
htmlRequestId =
typeof headers[NEXT_HTML_REQUEST_ID_HEADER] === 'string'
? headers[NEXT_HTML_REQUEST_ID_HEADER]
: undefined
}
return { return {
flightRouterState, flightRouterState,
...@@ -347,6 +363,7 @@ function parseRequestHeaders( ...@@ -347,6 +363,7 @@ function parseRequestHeaders(
isDevWarmupRequest, isDevWarmupRequest,
nonce, nonce,
previouslyRevalidatedTags, previouslyRevalidatedTags,
requestId,
htmlRequestId, htmlRequestId,
} }
} }
...@@ -1632,22 +1649,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1632,22 +1649,7 @@ async function renderToHTMLOrFlightImpl(
const { isStaticGeneration } = workStore const { isStaticGeneration } = workStore
let requestId: string let requestId: string
let htmlRequestId: string
if (isStaticGeneration) {
requestId = Buffer.from(
await crypto.subtle.digest('SHA-1', Buffer.from(req.url))
).toString('hex')
} else if (process.env.NEXT_RUNTIME === 'edge') {
requestId = crypto.randomUUID()
} else {
requestId = (
require('next/dist/compiled/nanoid') as typeof import('next/dist/compiled/nanoid')
).nanoid()
}
if (process.env.NODE_ENV !== 'production') {
res.setHeader(NEXT_REQUEST_ID_HEADER, requestId)
}
const { const {
flightRouterState, flightRouterState,
...@@ -1657,9 +1659,33 @@ async function renderToHTMLOrFlightImpl( ...@@ -1657,9 +1659,33 @@ async function renderToHTMLOrFlightImpl(
isDevWarmupRequest, isDevWarmupRequest,
isHmrRefresh, isHmrRefresh,
nonce, nonce,
htmlRequestId = requestId,
} = parsedRequestHeaders } = parsedRequestHeaders
if (parsedRequestHeaders.requestId) {
// If the client has provided a request ID (in development mode), we use it.
requestId = parsedRequestHeaders.requestId
} else {
// Otherwise we generate a new request ID.
if (isStaticGeneration) {
requestId = Buffer.from(
await crypto.subtle.digest('SHA-1', Buffer.from(req.url))
).toString('hex')
} else if (process.env.NEXT_RUNTIME === 'edge') {
requestId = crypto.randomUUID()
} else {
requestId = (
require('next/dist/compiled/nanoid') as typeof import('next/dist/compiled/nanoid')
).nanoid()
}
}
// If the client has provided an HTML request ID, we use it to associate the
// request with the HTML document from which it originated, which is used to
// send debug information to the associated WebSocket client. Otherwise, this
// is the request for the HTML document, so we use the request ID also as the
// HTML request ID.
htmlRequestId = parsedRequestHeaders.htmlRequestId || requestId
/** /**
* Dynamic parameters. E.g. when you visit `/dashboard/vercel` which is rendered by `/dashboard/[slug]` the value will be {"slug": "vercel"}. * Dynamic parameters. E.g. when you visit `/dashboard/vercel` which is rendered by `/dashboard/[slug]` the value will be {"slug": "vercel"}.
*/ */
......
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