Unverified Commit bedf3665 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[refactor] Replace runtime prefetch sentinel transform stream (#90160)

Runtime prefetch responses previously encoded `isPartial` and
`staleTime` using a sentinel-and-replace pattern: a random number was
embedded in the RSC payload during prerendering, then a TransformStream
scanned the prelude stream to find and replace it with the actual
values. This was fragile because it relied on matching a specific byte
sequence in a chunked stream that could be split at arbitrary
boundaries.

This replaces that mechanism with two simpler approaches:

- **isPartial**: A single byte prepended to the response stream (`#` for
complete, `~` for partial).

- **staleTime**: An `AsyncIterable<number>` in the Flight payload that
yields updated stale time values as the prerender store changes. This
ensures the latest value is always serialized in the stream, even when
sync IO aborts the prerender before the abort callback runs.

The stale time tracking logic is extracted into a new `stale-time.ts`
module with `StaleTimeIterable`, `trackStaleTime`, and
`finishStaleTimeTracking`.

On the client, dynamic runtime prefetch responses (detected via the
absence of `NEXT_IS_PRERENDER_HEADER`) have the byte stripped before
Flight decoding, and the stale time is read by iterating the async
iterable. Static responses served from cache are handled unchanged.
parent f10b63d1
......@@ -14,6 +14,7 @@ import {
import {
NEXT_DID_POSTPONE_HEADER,
NEXT_INSTANT_PREFETCH_HEADER,
NEXT_IS_PRERENDER_HEADER,
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
NEXT_ROUTER_STALE_TIME_HEADER,
......@@ -2012,13 +2013,28 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
// Track when the network connection closes.
const closed = createPromiseWithResolvers<void>()
let isResponsePartial = false
let responseBody = response.body
// For dynamic runtime prefetches, strip the leading isPartial byte before
// passing the stream to Flight. Static responses (served from cache) don't
// have the byte. We detect them via NEXT_IS_PRERENDER_HEADER.
if (
fetchStrategy === FetchStrategy.PPRRuntime &&
!response.headers.get(NEXT_IS_PRERENDER_HEADER)
) {
const stripped = await stripIsPartialByte(responseBody)
isResponsePartial = stripped.isPartial
responseBody = stripped.stream
}
let fulfilledEntries: Array<FulfilledSegmentCacheEntry> | null = null
const prefetchStream = createPrefetchResponseStream(
response.body,
responseBody,
closed.resolve,
function onResponseSizeUpdate(totalBytesReceivedSoFar) {
// When processing a dynamic response, we don't know how large each
// individual segment is, so approximate by assiging each segment
// individual segment is, so approximate by assigning each segment
// the average of the total response size.
if (fulfilledEntries === null) {
// Haven't received enough data yet to know which segments
......@@ -2038,13 +2054,6 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
{ allowPartialStream: true }
)
const isResponsePartial =
fetchStrategy === FetchStrategy.PPRRuntime
? // A runtime prefetch may have holes.
serverData.rp?.[0] === true
: // Full and LoadingBoundary prefetches cannot have holes.
// (even if we did set the prefetch header, we only use this codepath for non-PPR-enabled routes)
false
// Read head vary params synchronously. Individual segments carry their
// own thenables in CacheNodeSeedData.
const headVaryParamsThenable = serverData.h
......@@ -2053,17 +2062,21 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
? readVaryParams(headVaryParamsThenable)
: null
const now = Date.now()
const staleAt = await getStaleAt(now, serverData, response)
// Aside from writing the data into the cache, this function also returns
// the entries that were fulfilled, so we can streamingly update their sizes
// in the LRU as more data comes in.
fulfilledEntries = writeDynamicRenderResponseIntoCache(
Date.now(),
now,
task,
fetchStrategy,
response as RSCResponse<NavigationFlightResponse>,
serverData,
isResponsePartial,
headVaryParams,
staleAt,
route,
spawnedEntries
)
......@@ -2166,6 +2179,7 @@ function writeDynamicTreeResponseIntoCache(
serverData,
isResponsePartial,
headVaryParams,
getStaleAtFromHeader(now, response),
fulfilledEntry,
null
)
......@@ -2197,6 +2211,7 @@ function writeDynamicRenderResponseIntoCache(
serverData: NavigationFlightResponse,
isResponsePartial: boolean,
headVaryParams: VaryParams | null,
staleAt: number,
route: FulfilledRouteCacheEntry,
spawnedEntries: Map<SegmentRequestKey, PendingSegmentCacheEntry> | null
): Array<FulfilledSegmentCacheEntry> | null {
......@@ -2219,17 +2234,6 @@ function writeDynamicRenderResponseIntoCache(
return null
}
// For runtime prefetches, stale time is in the payload at rp[1].
// For other responses, fall back to the header.
const staleTimeSeconds =
typeof serverData.rp?.[1] === 'number'
? serverData.rp[1]
: parseInt(response.headers.get(NEXT_ROUTER_STALE_TIME_HEADER) ?? '', 10)
const staleTimeMs = !isNaN(staleTimeSeconds)
? getStaleTimeMs(staleTimeSeconds)
: STATIC_STALETIME_MS
const staleAt = now + staleTimeMs
for (const flightData of flightDatas) {
const seedData = flightData.seedData
if (seedData !== null) {
......@@ -2590,3 +2594,89 @@ function addInstantPrefetchHeaderIfLocked(
}
}
}
function getStaleAtFromHeader(
now: number,
response: RSCResponse<unknown>
): number {
const staleTimeSeconds = parseInt(
response.headers.get(NEXT_ROUTER_STALE_TIME_HEADER) ?? '',
10
)
const staleTimeMs = !isNaN(staleTimeSeconds)
? getStaleTimeMs(staleTimeSeconds)
: STATIC_STALETIME_MS
return now + staleTimeMs
}
async function getStaleAt(
now: number,
serverData: NavigationFlightResponse,
response: RSCResponse<unknown>
): Promise<number> {
if (serverData.s !== undefined) {
// Iterate the async iterable and take the last yielded value. The server
// yields updated staleTime values during the render; the last one is the
// final staleTime.
let staleTimeSeconds: number | undefined
// TODO: Buffer the response and then read the iterable values
// synchronously, similar to readVaryParams. This would avoid the need to
// make getStaleAt async, and we could also use it in
// writeDynamicTreeResponseIntoCache. This will also be needed when React
// starts leaving async iterables hanging when the outer RSC stream is
// aborted e.g. due to sync I/O (with unstable_allowPartialStream).
for await (const value of serverData.s) {
staleTimeSeconds = value
}
if (staleTimeSeconds !== undefined) {
const staleTimeMs = isNaN(staleTimeSeconds)
? STATIC_STALETIME_MS
: getStaleTimeMs(staleTimeSeconds)
return now + staleTimeMs
}
}
return getStaleAtFromHeader(now, response)
}
/**
* Strips the leading isPartial byte from a runtime prefetch response stream.
* Returns the remaining stream and whether the response is partial.
*/
async function stripIsPartialByte(
stream: ReadableStream<Uint8Array>
): Promise<{ stream: ReadableStream<Uint8Array>; isPartial: boolean }> {
const reader = stream.getReader()
const { done, value } = await reader.read()
if (done || !value || value.byteLength === 0) {
return {
stream: new ReadableStream({ start: (c) => c.close() }),
isPartial: false,
}
}
const isPartial = value[0] === 0x7e // ASCII '~'
const remainder = value.byteLength > 1 ? value.subarray(1) : null
return {
isPartial,
stream: new ReadableStream<Uint8Array>({
start(controller) {
if (remainder) {
controller.enqueue(remainder)
}
},
async pull(controller) {
const result = await reader.read()
if (result.done) {
controller.close()
} else {
controller.enqueue(result.value)
}
},
}),
}
}
import type { ExperimentalConfig } from '../config-shared'
import { INFINITE_CACHE } from '../../lib/constants'
/**
* An AsyncIterable<number> that yields staleTime values. Each call to
* `update()` yields the new value. When `close()` is called, the iteration
* ends.
*
* This is included in the RSC payload so Flight serializes each yielded value
* into the stream immediately. If the prerender is aborted by sync IO, the last
* yielded value is already in the stream, allowing the prerender to be aborted
* synchronously.
*/
export class StaleTimeIterable {
private _resolve: ((result: IteratorResult<number>) => void) | null = null
private _done = false
/** The last value passed to `update()`. */
public currentValue: number = 0
update(value: number): void {
if (this._done) return
this.currentValue = value
if (this._resolve) {
this._resolve({ value, done: false })
this._resolve = null
}
}
close(): void {
if (this._done) return
this._done = true
if (this._resolve) {
this._resolve({ value: undefined, done: true })
this._resolve = null
}
}
[Symbol.asyncIterator](): AsyncIterator<number> {
return {
next: () => {
if (this._done) {
return Promise.resolve({ value: undefined, done: true })
}
return new Promise<IteratorResult<number>>((resolve) => {
this._resolve = resolve
})
},
}
}
}
export function createSelectStaleTime(experimental: ExperimentalConfig) {
return (stale: number) =>
stale === INFINITE_CACHE &&
typeof experimental.staleTimes?.static === 'number'
? experimental.staleTimes.static
: stale
}
/**
* Intercepts writes to the `stale` field on the prerender store and yields
* each update (after applying selectStaleTime) through the iterable. This
* ensures the latest stale time is always serialized in the Flight stream,
* even if the prerender is aborted by sync IO.
*/
export function trackStaleTime(
store: { stale: number },
iterable: StaleTimeIterable,
selectStaleTime: (stale: number) => number
): void {
let _stale = store.stale
iterable.update(selectStaleTime(_stale))
Object.defineProperty(store, 'stale', {
get: () => _stale,
set: (value: number) => {
_stale = value
iterable.update(selectStaleTime(value))
},
configurable: true,
enumerable: true,
})
}
/**
* Closes the stale time iterable and waits for React to flush the closing
* chunk into the Flight stream. This also allows the prerender to complete if
* no other work is pending.
*
* Flight's internal work gets scheduled as a microtask when we close the
* iterable. We need to ensure Flight's pending queues are emptied before this
* function returns, because the caller will abort the prerender immediately
* after. We can't use a macrotask (that would allow dynamic IO to sneak into
* the response), so we use microtasks instead. The exact number of awaits
* isn't important as long as we wait enough ticks for Flight to finish writing.
*/
export async function finishStaleTimeTracking(
iterable: StaleTimeIterable
): Promise<void> {
iterable.close()
await Promise.resolve()
await Promise.resolve()
await Promise.resolve()
}
......@@ -276,8 +276,8 @@ export type NavigationFlightResponse = {
q: string
/** couldBeIntercepted */
i: boolean
/** runtimePrefetch - [isPartial, staleTime]. Only present in runtime prefetch responses. */
rp?: [boolean, number]
/** staleTime - Only present in dynamic runtime prefetch responses. */
s?: AsyncIterable<number>
/** headVaryParams */
h: VaryParamsThenable | null
}
......
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