Unverified Commit 88f07ed3 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Refactor the navigation fetch and decode layering (#99618)

Navigation and prefetch requests go through `createFetch`, which had
grown several jobs beyond fetching. It decoded navigation responses
itself (so React DevTools could time the request from the moment it was
sent), with a flag for prefetches to opt out and a separate wrapper
stream for HMR refreshes that can be superseded. With Cache Components,
it also stripped the leading partial marker byte by rebuilding the
Response around the stripped stream, and tee'd every response so a full
prefetch could cut its shell from a second copy of the body. Every
caller paid for the tee and the rebuilt Response, including ones that
never needed them.

This PR untangles those layers. Each commit can be reviewed on its own.

This is mostly a restructuring, to make the subsequent PRs in the stack
easier. No meaningful outward behavior changes.
parent 4b85c280
......@@ -21,6 +21,7 @@ import { fetch } from '../../segment-cache/fetch'
// eslint-disable-next-line import/no-extraneous-dependencies
import {
createFromFetch as createFromFetchBrowser,
createFromReadableStream as createFromReadableStreamBrowser,
createTemporaryReferenceSet,
encodeReply,
} from 'react-server-dom-webpack/client'
......@@ -48,6 +49,7 @@ import {
invalidateEntirePrefetchCache,
segmentCacheMap,
} from '../../segment-cache/cache'
import { stripIsPartialByte } from '../fetch-server-response'
import { startRevalidationCooldown } from '../../segment-cache/scheduler'
import { getDeploymentId } from '../../../../shared/lib/deployment-id'
import { getNavigationBuildId } from '../../../navigation-build-id'
......@@ -68,7 +70,7 @@ import {
} from '../../../../shared/lib/action-revalidation-kind'
import { isExternalURL } from '../../app-router-utils'
import { FreshnessPolicy, getCurrentNavigationLock } from '../../render-tree'
import { processFetch } from '../fetch-server-response'
import { InvariantError } from '../../../../shared/lib/invariant-error'
import {
invalidateBfCache,
UnknownDynamicStaleTime,
......@@ -76,6 +78,8 @@ import {
const createFromFetch =
createFromFetchBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromFetch']
const createFromReadableStream =
createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream']
let createDebugChannel:
| typeof import('../../../dev/debug-channel').createDebugChannel
......@@ -244,22 +248,33 @@ async function fetchServerAction(
let couldBeIntercepted: boolean = false
if (isRscResponse) {
// Server action redirect responses carry the Flight data of the redirect
// target, which may be prerendered with a completeness marker byte
// prepended. Strip it before passing to Flight.
const responsePromise = redirectLocation
? processFetch(res).then(({ response: r }) => r)
: Promise.resolve(res)
const response: ActionFlightResponse = await createFromFetch(
responsePromise,
{
callServer,
findSourceMapURL,
temporaryReferences,
debugChannel: createDebugChannel && createDebugChannel(headers),
const flightOptions = {
callServer,
findSourceMapURL,
temporaryReferences,
debugChannel: createDebugChannel && createDebugChannel(headers),
}
let response: ActionFlightResponse
if (process.env.__NEXT_CACHE_COMPONENTS && redirectLocation) {
// Server action redirect responses carry the Flight data of the redirect
// target, which may be prerendered with a completeness marker byte
// prepended. Strip it before passing to Flight.
if (!res.body) {
throw new InvariantError(
'Expected RSC navigation response to have a body'
)
}
)
const { stream } = await stripIsPartialByte(res.body)
response = await createFromReadableStream<ActionFlightResponse>(
stream,
flightOptions
)
} else {
response = await createFromFetch<ActionFlightResponse>(
Promise.resolve(res),
flightOptions
)
}
// An internal redirect can send an RSC response, but does not have a useful `actionResult`.
actionResult = redirectLocation ? undefined : response.a
......
......@@ -29,7 +29,7 @@ export function normalizeRenderedSearch(search: string): NormalizedSearch {
}
export function getRenderedSearch(
response: RSCResponse<unknown> | Response
response: RSCResponse | Response
): NormalizedSearch {
// If the server performed a rewrite, the search params used to render the
// page will be different from the params in the request URL. In this case,
......@@ -46,7 +46,7 @@ export function getRenderedSearch(
}
export function getRenderedPathname(
response: RSCResponse<unknown> | Response
response: RSCResponse | Response
): NormalizedPathname {
// If the server performed a rewrite, the pathname used to render the
// page will be different from the pathname in the request URL. In this case,
......
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