|
|
import type { |
|
|
TreePrefetch, |
|
|
RootTreePrefetch, |
|
|
SegmentPrefetch, |
|
|
} from '../../../server/app-render/collect-segment-data' |
|
|
import type { |
|
|
HeadData, |
|
|
LoadingModuleData, |
|
|
} from '../../../shared/lib/app-router-context.shared-runtime' |
|
|
import type { |
|
|
CacheNodeSeedData, |
|
|
Segment as FlightRouterStateSegment, |
|
|
} from '../../../server/app-render/types' |
|
|
import { HasLoadingBoundary } from '../../../server/app-render/types' |
|
|
import { |
|
|
NEXT_DID_POSTPONE_HEADER, |
|
|
NEXT_ROUTER_PREFETCH_HEADER, |
|
|
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, |
|
|
NEXT_ROUTER_STALE_TIME_HEADER, |
|
|
NEXT_ROUTER_STATE_TREE_HEADER, |
|
|
NEXT_URL, |
|
|
RSC_CONTENT_TYPE_HEADER, |
|
|
RSC_HEADER, |
|
|
} from '../app-router-headers' |
|
|
import { |
|
|
createFetch, |
|
|
createFromNextReadableStream, |
|
|
type RSCResponse, |
|
|
type RequestHeaders, |
|
|
} from '../router-reducer/fetch-server-response' |
|
|
import { |
|
|
pingPrefetchTask, |
|
|
isPrefetchTaskDirty, |
|
|
type PrefetchTask, |
|
|
type PrefetchSubtaskResult, |
|
|
} from './scheduler' |
|
|
import { getAppBuildId } from '../../app-build-id' |
|
|
import { createHrefFromUrl } from '../router-reducer/create-href-from-url' |
|
|
import type { |
|
|
NormalizedHref, |
|
|
NormalizedNextUrl, |
|
|
NormalizedSearch, |
|
|
RouteCacheKey, |
|
|
} from './cache-key' |
|
|
import { getRenderedSearch } from './cache-key' |
|
|
import { createTupleMap, type TupleMap, type Prefix } from './tuple-map' |
|
|
import { createLRU } from './lru' |
|
|
import { |
|
|
convertSegmentPathToStaticExportFilename, |
|
|
encodeChildSegmentKey, |
|
|
encodeSegment, |
|
|
ROOT_SEGMENT_KEY, |
|
|
} from '../../../shared/lib/segment-cache/segment-value-encoding' |
|
|
import type { |
|
|
FlightRouterState, |
|
|
NavigationFlightResponse, |
|
|
} from '../../../server/app-render/types' |
|
|
import { normalizeFlightData } from '../../flight-data-helpers' |
|
|
import { STATIC_STALETIME_MS } from '../router-reducer/prefetch-cache-utils' |
|
|
import { pingVisibleLinks } from '../links' |
|
|
import { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment' |
|
|
import { |
|
|
DOC_PREFETCH_RANGE_HEADER_VALUE, |
|
|
doesExportedHtmlMatchBuildId, |
|
|
} from '../../../shared/lib/segment-cache/output-export-prefetch-encoding' |
|
|
import { FetchStrategy } from '../segment-cache' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type RouteTree = { |
|
|
key: string |
|
|
segment: FlightRouterStateSegment |
|
|
slots: null | { |
|
|
[parallelRouteKey: string]: RouteTree |
|
|
} |
|
|
isRootLayout: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasLoadingBoundary: HasLoadingBoundary |
|
|
} |
|
|
|
|
|
type RouteCacheEntryShared = { |
|
|
staleAt: number |
|
|
|
|
|
|
|
|
|
|
|
couldBeIntercepted: boolean |
|
|
|
|
|
|
|
|
keypath: null | Prefix<RouteCacheKeypath> |
|
|
next: null | RouteCacheEntry |
|
|
prev: null | RouteCacheEntry |
|
|
size: number |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const enum EntryStatus { |
|
|
Empty, |
|
|
Pending, |
|
|
Fulfilled, |
|
|
Rejected, |
|
|
} |
|
|
|
|
|
type PendingRouteCacheEntry = RouteCacheEntryShared & { |
|
|
status: EntryStatus.Empty | EntryStatus.Pending |
|
|
blockedTasks: Set<PrefetchTask> | null |
|
|
canonicalUrl: null |
|
|
renderedSearch: null |
|
|
tree: null |
|
|
head: HeadData | null |
|
|
isHeadPartial: true |
|
|
isPPREnabled: false |
|
|
} |
|
|
|
|
|
type RejectedRouteCacheEntry = RouteCacheEntryShared & { |
|
|
status: EntryStatus.Rejected |
|
|
blockedTasks: Set<PrefetchTask> | null |
|
|
canonicalUrl: null |
|
|
renderedSearch: null |
|
|
tree: null |
|
|
head: null |
|
|
isHeadPartial: true |
|
|
isPPREnabled: boolean |
|
|
} |
|
|
|
|
|
export type FulfilledRouteCacheEntry = RouteCacheEntryShared & { |
|
|
status: EntryStatus.Fulfilled |
|
|
blockedTasks: null |
|
|
canonicalUrl: string |
|
|
renderedSearch: NormalizedSearch |
|
|
tree: RouteTree |
|
|
head: HeadData |
|
|
isHeadPartial: boolean |
|
|
isPPREnabled: boolean |
|
|
} |
|
|
|
|
|
export type RouteCacheEntry = |
|
|
| PendingRouteCacheEntry |
|
|
| FulfilledRouteCacheEntry |
|
|
| RejectedRouteCacheEntry |
|
|
|
|
|
type SegmentCacheEntryShared = { |
|
|
staleAt: number |
|
|
fetchStrategy: FetchStrategy |
|
|
revalidating: SegmentCacheEntry | null |
|
|
|
|
|
|
|
|
keypath: null | Prefix<SegmentCacheKeypath> |
|
|
next: null | SegmentCacheEntry |
|
|
prev: null | SegmentCacheEntry |
|
|
size: number |
|
|
} |
|
|
|
|
|
export type EmptySegmentCacheEntry = SegmentCacheEntryShared & { |
|
|
status: EntryStatus.Empty |
|
|
rsc: null |
|
|
loading: null |
|
|
isPartial: true |
|
|
promise: null |
|
|
} |
|
|
|
|
|
export type PendingSegmentCacheEntry = SegmentCacheEntryShared & { |
|
|
status: EntryStatus.Pending |
|
|
rsc: null |
|
|
loading: null |
|
|
isPartial: true |
|
|
promise: null | PromiseWithResolvers<FulfilledSegmentCacheEntry | null> |
|
|
} |
|
|
|
|
|
type RejectedSegmentCacheEntry = SegmentCacheEntryShared & { |
|
|
status: EntryStatus.Rejected |
|
|
rsc: null |
|
|
loading: null |
|
|
isPartial: true |
|
|
promise: null |
|
|
} |
|
|
|
|
|
export type FulfilledSegmentCacheEntry = SegmentCacheEntryShared & { |
|
|
status: EntryStatus.Fulfilled |
|
|
rsc: React.ReactNode | null |
|
|
loading: LoadingModuleData | Promise<LoadingModuleData> |
|
|
isPartial: boolean |
|
|
promise: null |
|
|
} |
|
|
|
|
|
export type SegmentCacheEntry = |
|
|
| EmptySegmentCacheEntry |
|
|
| PendingSegmentCacheEntry |
|
|
| RejectedSegmentCacheEntry |
|
|
| FulfilledSegmentCacheEntry |
|
|
|
|
|
export type NonEmptySegmentCacheEntry = Exclude< |
|
|
SegmentCacheEntry, |
|
|
EmptySegmentCacheEntry |
|
|
> |
|
|
|
|
|
const isOutputExportMode = |
|
|
process.env.NODE_ENV === 'production' && |
|
|
process.env.__NEXT_CONFIG_OUTPUT === 'export' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type RouteCacheKeypath = [NormalizedHref, NormalizedNextUrl] |
|
|
let routeCacheMap: TupleMap<RouteCacheKeypath, RouteCacheEntry> = |
|
|
createTupleMap() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const maxRouteLruSize = 10 * 1024 * 1024 |
|
|
let routeCacheLru = createLRU<RouteCacheEntry>( |
|
|
maxRouteLruSize, |
|
|
onRouteLRUEviction |
|
|
) |
|
|
|
|
|
type SegmentCacheKeypath = [string, NormalizedSearch] |
|
|
let segmentCacheMap: TupleMap<SegmentCacheKeypath, SegmentCacheEntry> = |
|
|
createTupleMap() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const maxSegmentLruSize = 50 * 1024 * 1024 |
|
|
let segmentCacheLru = createLRU<SegmentCacheEntry>( |
|
|
maxSegmentLruSize, |
|
|
onSegmentLRUEviction |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let invalidationListeners: Set<PrefetchTask> | null = null |
|
|
|
|
|
|
|
|
let currentCacheVersion = 0 |
|
|
|
|
|
export function getCurrentCacheVersion(): number { |
|
|
return currentCacheVersion |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function revalidateEntireCache( |
|
|
nextUrl: string | null, |
|
|
tree: FlightRouterState |
|
|
) { |
|
|
currentCacheVersion++ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
routeCacheMap = createTupleMap() |
|
|
routeCacheLru = createLRU(maxRouteLruSize, onRouteLRUEviction) |
|
|
segmentCacheMap = createTupleMap() |
|
|
segmentCacheLru = createLRU(maxSegmentLruSize, onSegmentLRUEviction) |
|
|
|
|
|
|
|
|
pingVisibleLinks(nextUrl, tree) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pingInvalidationListeners(nextUrl, tree) |
|
|
} |
|
|
|
|
|
function attachInvalidationListener(task: PrefetchTask): void { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (task.onInvalidate !== null) { |
|
|
if (invalidationListeners === null) { |
|
|
invalidationListeners = new Set([task]) |
|
|
} else { |
|
|
invalidationListeners.add(task) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
function notifyInvalidationListener(task: PrefetchTask): void { |
|
|
const onInvalidate = task.onInvalidate |
|
|
if (onInvalidate !== null) { |
|
|
|
|
|
|
|
|
task.onInvalidate = null |
|
|
|
|
|
|
|
|
try { |
|
|
onInvalidate() |
|
|
} catch (error) { |
|
|
if (typeof reportError === 'function') { |
|
|
reportError(error) |
|
|
} else { |
|
|
console.error(error) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
export function pingInvalidationListeners( |
|
|
nextUrl: string | null, |
|
|
tree: FlightRouterState |
|
|
): void { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (invalidationListeners !== null) { |
|
|
const tasks = invalidationListeners |
|
|
invalidationListeners = null |
|
|
for (const task of tasks) { |
|
|
if (isPrefetchTaskDirty(task, nextUrl, tree)) { |
|
|
notifyInvalidationListener(task) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
export function readExactRouteCacheEntry( |
|
|
now: number, |
|
|
href: NormalizedHref, |
|
|
nextUrl: NormalizedNextUrl | null |
|
|
): RouteCacheEntry | null { |
|
|
const keypath: Prefix<RouteCacheKeypath> = |
|
|
nextUrl === null ? [href] : [href, nextUrl] |
|
|
const existingEntry = routeCacheMap.get(keypath) |
|
|
if (existingEntry !== null) { |
|
|
|
|
|
if (existingEntry.staleAt > now) { |
|
|
|
|
|
|
|
|
|
|
|
routeCacheLru.put(existingEntry) |
|
|
|
|
|
return existingEntry |
|
|
} else { |
|
|
|
|
|
deleteRouteFromCache(existingEntry, keypath) |
|
|
} |
|
|
} |
|
|
return null |
|
|
} |
|
|
|
|
|
export function readRouteCacheEntry( |
|
|
now: number, |
|
|
key: RouteCacheKey |
|
|
): RouteCacheEntry | null { |
|
|
|
|
|
|
|
|
const nonInterceptedEntry = readExactRouteCacheEntry(now, key.href, null) |
|
|
if (nonInterceptedEntry !== null && !nonInterceptedEntry.couldBeIntercepted) { |
|
|
|
|
|
return nonInterceptedEntry |
|
|
} |
|
|
|
|
|
return readExactRouteCacheEntry(now, key.href, key.nextUrl) |
|
|
} |
|
|
|
|
|
export function getSegmentKeypathForTask( |
|
|
task: PrefetchTask, |
|
|
route: FulfilledRouteCacheEntry, |
|
|
path: string |
|
|
): Prefix<SegmentCacheKeypath> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isDynamicTask = |
|
|
task.fetchStrategy === FetchStrategy.Full || !route.isPPREnabled |
|
|
return isDynamicTask && path.endsWith('/' + PAGE_SEGMENT_KEY) |
|
|
? [path, route.renderedSearch] |
|
|
: [path] |
|
|
} |
|
|
|
|
|
export function readSegmentCacheEntry( |
|
|
now: number, |
|
|
route: FulfilledRouteCacheEntry, |
|
|
path: string |
|
|
): SegmentCacheEntry | null { |
|
|
if (!path.endsWith('/' + PAGE_SEGMENT_KEY)) { |
|
|
|
|
|
return readExactSegmentCacheEntry(now, [path]) |
|
|
} |
|
|
|
|
|
const renderedSearch = route.renderedSearch |
|
|
if (renderedSearch !== null) { |
|
|
|
|
|
|
|
|
|
|
|
const entryWithSearchParams = readExactSegmentCacheEntry(now, [ |
|
|
path, |
|
|
renderedSearch, |
|
|
]) |
|
|
if (entryWithSearchParams !== null) { |
|
|
return entryWithSearchParams |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const entryWithoutSearchParams = readExactSegmentCacheEntry(now, [path]) |
|
|
return entryWithoutSearchParams |
|
|
} |
|
|
|
|
|
function readExactSegmentCacheEntry( |
|
|
now: number, |
|
|
keypath: Prefix<SegmentCacheKeypath> |
|
|
): SegmentCacheEntry | null { |
|
|
const existingEntry = segmentCacheMap.get(keypath) |
|
|
if (existingEntry !== null) { |
|
|
|
|
|
if (existingEntry.staleAt > now) { |
|
|
|
|
|
|
|
|
|
|
|
segmentCacheLru.put(existingEntry) |
|
|
|
|
|
return existingEntry |
|
|
} else { |
|
|
|
|
|
const revalidatingEntry = existingEntry.revalidating |
|
|
if (revalidatingEntry !== null) { |
|
|
|
|
|
const upsertedEntry = upsertSegmentEntry( |
|
|
now, |
|
|
keypath, |
|
|
revalidatingEntry |
|
|
) |
|
|
if (upsertedEntry !== null && upsertedEntry.staleAt > now) { |
|
|
|
|
|
return upsertedEntry |
|
|
} |
|
|
} else { |
|
|
|
|
|
deleteSegmentFromCache(existingEntry, keypath) |
|
|
} |
|
|
} |
|
|
} |
|
|
return null |
|
|
} |
|
|
|
|
|
function readRevalidatingSegmentCacheEntry( |
|
|
now: number, |
|
|
owner: SegmentCacheEntry |
|
|
): SegmentCacheEntry | null { |
|
|
const existingRevalidation = owner.revalidating |
|
|
if (existingRevalidation !== null) { |
|
|
if (existingRevalidation.staleAt > now) { |
|
|
|
|
|
|
|
|
return existingRevalidation |
|
|
} else { |
|
|
|
|
|
clearRevalidatingSegmentFromOwner(owner) |
|
|
} |
|
|
} |
|
|
return null |
|
|
} |
|
|
|
|
|
export function waitForSegmentCacheEntry( |
|
|
pendingEntry: PendingSegmentCacheEntry |
|
|
): Promise<FulfilledSegmentCacheEntry | null> { |
|
|
|
|
|
|
|
|
let promiseWithResolvers = pendingEntry.promise |
|
|
if (promiseWithResolvers === null) { |
|
|
promiseWithResolvers = pendingEntry.promise = |
|
|
createPromiseWithResolvers<FulfilledSegmentCacheEntry | null>() |
|
|
} else { |
|
|
|
|
|
} |
|
|
return promiseWithResolvers.promise |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function readOrCreateRouteCacheEntry( |
|
|
now: number, |
|
|
task: PrefetchTask |
|
|
): RouteCacheEntry { |
|
|
attachInvalidationListener(task) |
|
|
|
|
|
const key = task.key |
|
|
const existingEntry = readRouteCacheEntry(now, key) |
|
|
if (existingEntry !== null) { |
|
|
return existingEntry |
|
|
} |
|
|
|
|
|
const pendingEntry: PendingRouteCacheEntry = { |
|
|
canonicalUrl: null, |
|
|
status: EntryStatus.Empty, |
|
|
blockedTasks: null, |
|
|
tree: null, |
|
|
head: null, |
|
|
isHeadPartial: true, |
|
|
|
|
|
|
|
|
staleAt: Infinity, |
|
|
|
|
|
|
|
|
|
|
|
couldBeIntercepted: true, |
|
|
|
|
|
isPPREnabled: false, |
|
|
renderedSearch: null, |
|
|
|
|
|
|
|
|
keypath: null, |
|
|
next: null, |
|
|
prev: null, |
|
|
size: 0, |
|
|
} |
|
|
const keypath: Prefix<RouteCacheKeypath> = |
|
|
key.nextUrl === null ? [key.href] : [key.href, key.nextUrl] |
|
|
routeCacheMap.set(keypath, pendingEntry) |
|
|
|
|
|
|
|
|
pendingEntry.keypath = keypath |
|
|
routeCacheLru.put(pendingEntry) |
|
|
return pendingEntry |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function readOrCreateSegmentCacheEntry( |
|
|
now: number, |
|
|
task: PrefetchTask, |
|
|
route: FulfilledRouteCacheEntry, |
|
|
path: string |
|
|
): SegmentCacheEntry { |
|
|
const keypath = getSegmentKeypathForTask(task, route, path) |
|
|
const existingEntry = readExactSegmentCacheEntry(now, keypath) |
|
|
if (existingEntry !== null) { |
|
|
return existingEntry |
|
|
} |
|
|
|
|
|
const pendingEntry = createDetachedSegmentCacheEntry(route.staleAt) |
|
|
segmentCacheMap.set(keypath, pendingEntry) |
|
|
|
|
|
|
|
|
pendingEntry.keypath = keypath |
|
|
segmentCacheLru.put(pendingEntry) |
|
|
return pendingEntry |
|
|
} |
|
|
|
|
|
export function readOrCreateRevalidatingSegmentEntry( |
|
|
now: number, |
|
|
prevEntry: SegmentCacheEntry |
|
|
): SegmentCacheEntry { |
|
|
const existingRevalidation = readRevalidatingSegmentCacheEntry(now, prevEntry) |
|
|
if (existingRevalidation !== null) { |
|
|
return existingRevalidation |
|
|
} |
|
|
const pendingEntry = createDetachedSegmentCacheEntry(prevEntry.staleAt) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prevEntry.revalidating = pendingEntry |
|
|
|
|
|
return pendingEntry |
|
|
} |
|
|
|
|
|
export function upsertSegmentEntry( |
|
|
now: number, |
|
|
keypath: Prefix<SegmentCacheKeypath>, |
|
|
candidateEntry: SegmentCacheEntry |
|
|
): SegmentCacheEntry | null { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingEntry = readExactSegmentCacheEntry(now, keypath) |
|
|
if (existingEntry !== null) { |
|
|
if (candidateEntry.isPartial && !existingEntry.isPartial) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rejectedEntry: RejectedSegmentCacheEntry = candidateEntry as any |
|
|
rejectedEntry.status = EntryStatus.Rejected |
|
|
rejectedEntry.loading = null |
|
|
rejectedEntry.rsc = null |
|
|
return null |
|
|
} |
|
|
|
|
|
deleteSegmentFromCache(existingEntry, keypath) |
|
|
} |
|
|
segmentCacheMap.set(keypath, candidateEntry) |
|
|
|
|
|
|
|
|
candidateEntry.keypath = keypath |
|
|
segmentCacheLru.put(candidateEntry) |
|
|
return candidateEntry |
|
|
} |
|
|
|
|
|
export function createDetachedSegmentCacheEntry( |
|
|
staleAt: number |
|
|
): EmptySegmentCacheEntry { |
|
|
const emptyEntry: EmptySegmentCacheEntry = { |
|
|
status: EntryStatus.Empty, |
|
|
|
|
|
|
|
|
fetchStrategy: FetchStrategy.PPR, |
|
|
revalidating: null, |
|
|
rsc: null, |
|
|
loading: null, |
|
|
staleAt, |
|
|
isPartial: true, |
|
|
promise: null, |
|
|
|
|
|
|
|
|
keypath: null, |
|
|
next: null, |
|
|
prev: null, |
|
|
size: 0, |
|
|
} |
|
|
return emptyEntry |
|
|
} |
|
|
|
|
|
export function upgradeToPendingSegment( |
|
|
emptyEntry: EmptySegmentCacheEntry, |
|
|
fetchStrategy: FetchStrategy |
|
|
): PendingSegmentCacheEntry { |
|
|
const pendingEntry: PendingSegmentCacheEntry = emptyEntry as any |
|
|
pendingEntry.status = EntryStatus.Pending |
|
|
pendingEntry.fetchStrategy = fetchStrategy |
|
|
return pendingEntry |
|
|
} |
|
|
|
|
|
function deleteRouteFromCache( |
|
|
entry: RouteCacheEntry, |
|
|
keypath: Prefix<RouteCacheKeypath> |
|
|
): void { |
|
|
pingBlockedTasks(entry) |
|
|
routeCacheMap.delete(keypath) |
|
|
routeCacheLru.delete(entry) |
|
|
} |
|
|
|
|
|
function deleteSegmentFromCache( |
|
|
entry: SegmentCacheEntry, |
|
|
keypath: Prefix<SegmentCacheKeypath> |
|
|
): void { |
|
|
cancelEntryListeners(entry) |
|
|
segmentCacheMap.delete(keypath) |
|
|
segmentCacheLru.delete(entry) |
|
|
clearRevalidatingSegmentFromOwner(entry) |
|
|
} |
|
|
|
|
|
function clearRevalidatingSegmentFromOwner(owner: SegmentCacheEntry): void { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const revalidatingSegment = owner.revalidating |
|
|
if (revalidatingSegment !== null) { |
|
|
cancelEntryListeners(revalidatingSegment) |
|
|
owner.revalidating = null |
|
|
} |
|
|
} |
|
|
|
|
|
export function resetRevalidatingSegmentEntry( |
|
|
owner: SegmentCacheEntry |
|
|
): EmptySegmentCacheEntry { |
|
|
clearRevalidatingSegmentFromOwner(owner) |
|
|
const emptyEntry = createDetachedSegmentCacheEntry(owner.staleAt) |
|
|
owner.revalidating = emptyEntry |
|
|
return emptyEntry |
|
|
} |
|
|
|
|
|
function onRouteLRUEviction(entry: RouteCacheEntry): void { |
|
|
|
|
|
const keypath = entry.keypath |
|
|
if (keypath !== null) { |
|
|
entry.keypath = null |
|
|
pingBlockedTasks(entry) |
|
|
routeCacheMap.delete(keypath) |
|
|
} |
|
|
} |
|
|
|
|
|
function onSegmentLRUEviction(entry: SegmentCacheEntry): void { |
|
|
|
|
|
const keypath = entry.keypath |
|
|
if (keypath !== null) { |
|
|
entry.keypath = null |
|
|
cancelEntryListeners(entry) |
|
|
segmentCacheMap.delete(keypath) |
|
|
} |
|
|
} |
|
|
|
|
|
function cancelEntryListeners(entry: SegmentCacheEntry): void { |
|
|
if (entry.status === EntryStatus.Pending && entry.promise !== null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
entry.promise.resolve(null) |
|
|
entry.promise = null |
|
|
} |
|
|
} |
|
|
|
|
|
function pingBlockedTasks(entry: { |
|
|
blockedTasks: Set<PrefetchTask> | null |
|
|
}): void { |
|
|
const blockedTasks = entry.blockedTasks |
|
|
if (blockedTasks !== null) { |
|
|
for (const task of blockedTasks) { |
|
|
pingPrefetchTask(task) |
|
|
} |
|
|
entry.blockedTasks = null |
|
|
} |
|
|
} |
|
|
|
|
|
function fulfillRouteCacheEntry( |
|
|
entry: RouteCacheEntry, |
|
|
tree: RouteTree, |
|
|
head: HeadData, |
|
|
isHeadPartial: boolean, |
|
|
staleAt: number, |
|
|
couldBeIntercepted: boolean, |
|
|
canonicalUrl: string, |
|
|
renderedSearch: NormalizedSearch, |
|
|
isPPREnabled: boolean |
|
|
): FulfilledRouteCacheEntry { |
|
|
const fulfilledEntry: FulfilledRouteCacheEntry = entry as any |
|
|
fulfilledEntry.status = EntryStatus.Fulfilled |
|
|
fulfilledEntry.tree = tree |
|
|
fulfilledEntry.head = head |
|
|
fulfilledEntry.isHeadPartial = isHeadPartial |
|
|
fulfilledEntry.staleAt = staleAt |
|
|
fulfilledEntry.couldBeIntercepted = couldBeIntercepted |
|
|
fulfilledEntry.canonicalUrl = canonicalUrl |
|
|
fulfilledEntry.renderedSearch = renderedSearch |
|
|
fulfilledEntry.isPPREnabled = isPPREnabled |
|
|
pingBlockedTasks(entry) |
|
|
return fulfilledEntry |
|
|
} |
|
|
|
|
|
function fulfillSegmentCacheEntry( |
|
|
segmentCacheEntry: PendingSegmentCacheEntry, |
|
|
rsc: React.ReactNode, |
|
|
loading: LoadingModuleData | Promise<LoadingModuleData>, |
|
|
staleAt: number, |
|
|
isPartial: boolean |
|
|
): FulfilledSegmentCacheEntry { |
|
|
const fulfilledEntry: FulfilledSegmentCacheEntry = segmentCacheEntry as any |
|
|
fulfilledEntry.status = EntryStatus.Fulfilled |
|
|
fulfilledEntry.rsc = rsc |
|
|
fulfilledEntry.loading = loading |
|
|
fulfilledEntry.staleAt = staleAt |
|
|
fulfilledEntry.isPartial = isPartial |
|
|
|
|
|
if (segmentCacheEntry.promise !== null) { |
|
|
segmentCacheEntry.promise.resolve(fulfilledEntry) |
|
|
|
|
|
fulfilledEntry.promise = null |
|
|
} |
|
|
return fulfilledEntry |
|
|
} |
|
|
|
|
|
function rejectRouteCacheEntry( |
|
|
entry: PendingRouteCacheEntry, |
|
|
staleAt: number |
|
|
): void { |
|
|
const rejectedEntry: RejectedRouteCacheEntry = entry as any |
|
|
rejectedEntry.status = EntryStatus.Rejected |
|
|
rejectedEntry.staleAt = staleAt |
|
|
pingBlockedTasks(entry) |
|
|
} |
|
|
|
|
|
function rejectSegmentCacheEntry( |
|
|
entry: PendingSegmentCacheEntry, |
|
|
staleAt: number |
|
|
): void { |
|
|
const rejectedEntry: RejectedSegmentCacheEntry = entry as any |
|
|
rejectedEntry.status = EntryStatus.Rejected |
|
|
rejectedEntry.staleAt = staleAt |
|
|
if (entry.promise !== null) { |
|
|
|
|
|
|
|
|
entry.promise.resolve(null) |
|
|
entry.promise = null |
|
|
} |
|
|
} |
|
|
|
|
|
function convertRootTreePrefetchToRouteTree(rootTree: RootTreePrefetch) { |
|
|
return convertTreePrefetchToRouteTree(rootTree.tree, ROOT_SEGMENT_KEY) |
|
|
} |
|
|
|
|
|
function convertTreePrefetchToRouteTree( |
|
|
prefetch: TreePrefetch, |
|
|
key: string |
|
|
): RouteTree { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let slots: { [parallelRouteKey: string]: RouteTree } | null = null |
|
|
const prefetchSlots = prefetch.slots |
|
|
if (prefetchSlots !== null) { |
|
|
slots = {} |
|
|
for (let parallelRouteKey in prefetchSlots) { |
|
|
const childPrefetch = prefetchSlots[parallelRouteKey] |
|
|
const childSegment = childPrefetch.segment |
|
|
|
|
|
|
|
|
|
|
|
const childKey = encodeChildSegmentKey( |
|
|
key, |
|
|
parallelRouteKey, |
|
|
encodeSegment(childSegment) |
|
|
) |
|
|
slots[parallelRouteKey] = convertTreePrefetchToRouteTree( |
|
|
childPrefetch, |
|
|
childKey |
|
|
) |
|
|
} |
|
|
} |
|
|
return { |
|
|
key, |
|
|
segment: prefetch.segment, |
|
|
slots, |
|
|
isRootLayout: prefetch.isRootLayout, |
|
|
|
|
|
|
|
|
hasLoadingBoundary: HasLoadingBoundary.SegmentHasLoadingBoundary, |
|
|
} |
|
|
} |
|
|
|
|
|
function convertRootFlightRouterStateToRouteTree( |
|
|
flightRouterState: FlightRouterState |
|
|
): RouteTree { |
|
|
return convertFlightRouterStateToRouteTree( |
|
|
flightRouterState, |
|
|
ROOT_SEGMENT_KEY |
|
|
) |
|
|
} |
|
|
|
|
|
function convertFlightRouterStateToRouteTree( |
|
|
flightRouterState: FlightRouterState, |
|
|
key: string |
|
|
): RouteTree { |
|
|
let slots: { [parallelRouteKey: string]: RouteTree } | null = null |
|
|
|
|
|
const parallelRoutes = flightRouterState[1] |
|
|
for (let parallelRouteKey in parallelRoutes) { |
|
|
const childRouterState = parallelRoutes[parallelRouteKey] |
|
|
const childSegment = childRouterState[0] |
|
|
|
|
|
|
|
|
|
|
|
const childKey = encodeChildSegmentKey( |
|
|
key, |
|
|
parallelRouteKey, |
|
|
encodeSegment(childSegment) |
|
|
) |
|
|
const childTree = convertFlightRouterStateToRouteTree( |
|
|
childRouterState, |
|
|
childKey |
|
|
) |
|
|
if (slots === null) { |
|
|
slots = { |
|
|
[parallelRouteKey]: childTree, |
|
|
} |
|
|
} else { |
|
|
slots[parallelRouteKey] = childTree |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const originalSegment = flightRouterState[0] |
|
|
const segmentWithoutSearchParams = |
|
|
typeof originalSegment === 'string' && |
|
|
originalSegment.startsWith(PAGE_SEGMENT_KEY) |
|
|
? PAGE_SEGMENT_KEY |
|
|
: originalSegment |
|
|
|
|
|
return { |
|
|
key, |
|
|
segment: segmentWithoutSearchParams, |
|
|
slots, |
|
|
isRootLayout: flightRouterState[4] === true, |
|
|
hasLoadingBoundary: |
|
|
flightRouterState[5] !== undefined |
|
|
? flightRouterState[5] |
|
|
: HasLoadingBoundary.SubtreeHasNoLoadingBoundary, |
|
|
} |
|
|
} |
|
|
|
|
|
export function convertRouteTreeToFlightRouterState( |
|
|
routeTree: RouteTree |
|
|
): FlightRouterState { |
|
|
const parallelRoutes: Record<string, FlightRouterState> = {} |
|
|
if (routeTree.slots !== null) { |
|
|
for (const parallelRouteKey in routeTree.slots) { |
|
|
parallelRoutes[parallelRouteKey] = convertRouteTreeToFlightRouterState( |
|
|
routeTree.slots[parallelRouteKey] |
|
|
) |
|
|
} |
|
|
} |
|
|
const flightRouterState: FlightRouterState = [ |
|
|
routeTree.segment, |
|
|
parallelRoutes, |
|
|
null, |
|
|
null, |
|
|
routeTree.isRootLayout, |
|
|
] |
|
|
return flightRouterState |
|
|
} |
|
|
|
|
|
export async function fetchRouteOnCacheMiss( |
|
|
entry: PendingRouteCacheEntry, |
|
|
task: PrefetchTask |
|
|
): Promise<PrefetchSubtaskResult<null> | null> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const key = task.key |
|
|
const href = key.href |
|
|
const nextUrl = key.nextUrl |
|
|
const segmentPath = '/_tree' |
|
|
|
|
|
const headers: RequestHeaders = { |
|
|
[RSC_HEADER]: '1', |
|
|
[NEXT_ROUTER_PREFETCH_HEADER]: '1', |
|
|
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]: segmentPath, |
|
|
} |
|
|
if (nextUrl !== null) { |
|
|
headers[NEXT_URL] = nextUrl |
|
|
} |
|
|
|
|
|
try { |
|
|
let response |
|
|
let urlAfterRedirects |
|
|
if (isOutputExportMode) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const url = new URL(href) |
|
|
const htmlResponse = await fetch(href, { |
|
|
headers: { |
|
|
Range: DOC_PREFETCH_RANGE_HEADER_VALUE, |
|
|
}, |
|
|
}) |
|
|
const partialHtml = await htmlResponse.text() |
|
|
if (!doesExportedHtmlMatchBuildId(partialHtml, getAppBuildId())) { |
|
|
|
|
|
|
|
|
rejectRouteCacheEntry(entry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
urlAfterRedirects = htmlResponse.redirected |
|
|
? new URL(htmlResponse.url) |
|
|
: url |
|
|
response = await fetchPrefetchResponse( |
|
|
addSegmentPathToUrlInOutputExportMode(urlAfterRedirects, segmentPath), |
|
|
headers |
|
|
) |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const url = new URL(href) |
|
|
response = await fetchPrefetchResponse(url, headers) |
|
|
urlAfterRedirects = |
|
|
response !== null && response.redirected ? new URL(response.url) : url |
|
|
} |
|
|
|
|
|
if ( |
|
|
!response || |
|
|
!response.ok || |
|
|
|
|
|
|
|
|
|
|
|
response.status === 204 || |
|
|
!response.body |
|
|
) { |
|
|
|
|
|
|
|
|
rejectRouteCacheEntry(entry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canonicalUrl = createHrefFromUrl(urlAfterRedirects) |
|
|
|
|
|
|
|
|
const varyHeader = response.headers.get('vary') |
|
|
const couldBeIntercepted = |
|
|
varyHeader !== null && varyHeader.includes(NEXT_URL) |
|
|
|
|
|
|
|
|
const closed = createPromiseWithResolvers<void>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const routeIsPPREnabled = |
|
|
response.headers.get(NEXT_DID_POSTPONE_HEADER) === '2' || |
|
|
|
|
|
|
|
|
|
|
|
isOutputExportMode |
|
|
|
|
|
if (routeIsPPREnabled) { |
|
|
const prefetchStream = createPrefetchResponseStream( |
|
|
response.body, |
|
|
closed.resolve, |
|
|
function onResponseSizeUpdate(size) { |
|
|
routeCacheLru.updateSize(entry, size) |
|
|
} |
|
|
) |
|
|
const serverData = await (createFromNextReadableStream( |
|
|
prefetchStream |
|
|
) as Promise<RootTreePrefetch>) |
|
|
if (serverData.buildId !== getAppBuildId()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rejectRouteCacheEntry(entry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const renderedSearch = getRenderedSearch(response) |
|
|
|
|
|
const staleTimeMs = serverData.staleTime * 1000 |
|
|
fulfillRouteCacheEntry( |
|
|
entry, |
|
|
convertRootTreePrefetchToRouteTree(serverData), |
|
|
serverData.head, |
|
|
serverData.isHeadPartial, |
|
|
Date.now() + staleTimeMs, |
|
|
couldBeIntercepted, |
|
|
canonicalUrl, |
|
|
renderedSearch, |
|
|
routeIsPPREnabled |
|
|
) |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const prefetchStream = createPrefetchResponseStream( |
|
|
response.body, |
|
|
closed.resolve, |
|
|
function onResponseSizeUpdate(size) { |
|
|
routeCacheLru.updateSize(entry, size) |
|
|
} |
|
|
) |
|
|
const serverData = await (createFromNextReadableStream( |
|
|
prefetchStream |
|
|
) as Promise<NavigationFlightResponse>) |
|
|
if (serverData.b !== getAppBuildId()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rejectRouteCacheEntry(entry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
|
|
|
writeDynamicTreeResponseIntoCache( |
|
|
Date.now(), |
|
|
task, |
|
|
|
|
|
|
|
|
FetchStrategy.LoadingBoundary, |
|
|
response, |
|
|
serverData, |
|
|
entry, |
|
|
couldBeIntercepted, |
|
|
canonicalUrl, |
|
|
routeIsPPREnabled |
|
|
) |
|
|
} |
|
|
|
|
|
if (!couldBeIntercepted && nextUrl !== null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const currentKeypath: Prefix<RouteCacheKeypath> = [href, nextUrl] |
|
|
const expectedEntry = routeCacheMap.get(currentKeypath) |
|
|
if (expectedEntry === entry) { |
|
|
routeCacheMap.delete(currentKeypath) |
|
|
const newKeypath: Prefix<RouteCacheKeypath> = [href] |
|
|
routeCacheMap.set(newKeypath, entry) |
|
|
|
|
|
|
|
|
|
|
|
entry.keypath = newKeypath |
|
|
} else { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return { value: null, closed: closed.promise } |
|
|
} catch (error) { |
|
|
|
|
|
|
|
|
rejectRouteCacheEntry(entry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
} |
|
|
|
|
|
export async function fetchSegmentOnCacheMiss( |
|
|
route: FulfilledRouteCacheEntry, |
|
|
segmentCacheEntry: PendingSegmentCacheEntry, |
|
|
routeKey: RouteCacheKey, |
|
|
segmentPath: string |
|
|
): Promise<PrefetchSubtaskResult<FulfilledSegmentCacheEntry> | null> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const url = new URL(route.canonicalUrl, routeKey.href) |
|
|
const nextUrl = routeKey.nextUrl |
|
|
|
|
|
const normalizedSegmentPath = |
|
|
segmentPath === ROOT_SEGMENT_KEY |
|
|
? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'/_index' |
|
|
: segmentPath |
|
|
|
|
|
const headers: RequestHeaders = { |
|
|
[RSC_HEADER]: '1', |
|
|
[NEXT_ROUTER_PREFETCH_HEADER]: '1', |
|
|
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]: normalizedSegmentPath, |
|
|
} |
|
|
if (nextUrl !== null) { |
|
|
headers[NEXT_URL] = nextUrl |
|
|
} |
|
|
|
|
|
const requestUrl = isOutputExportMode |
|
|
? |
|
|
addSegmentPathToUrlInOutputExportMode(url, normalizedSegmentPath) |
|
|
: url |
|
|
try { |
|
|
const response = await fetchPrefetchResponse(requestUrl, headers) |
|
|
if ( |
|
|
!response || |
|
|
!response.ok || |
|
|
response.status === 204 || |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(response.headers.get(NEXT_DID_POSTPONE_HEADER) !== '2' && |
|
|
|
|
|
|
|
|
|
|
|
!isOutputExportMode) || |
|
|
!response.body |
|
|
) { |
|
|
|
|
|
|
|
|
rejectSegmentCacheEntry(segmentCacheEntry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
const closed = createPromiseWithResolvers<void>() |
|
|
|
|
|
|
|
|
|
|
|
const prefetchStream = createPrefetchResponseStream( |
|
|
response.body, |
|
|
closed.resolve, |
|
|
function onResponseSizeUpdate(size) { |
|
|
segmentCacheLru.updateSize(segmentCacheEntry, size) |
|
|
} |
|
|
) |
|
|
const serverData = await (createFromNextReadableStream( |
|
|
prefetchStream |
|
|
) as Promise<SegmentPrefetch>) |
|
|
if (serverData.buildId !== getAppBuildId()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rejectSegmentCacheEntry(segmentCacheEntry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
return { |
|
|
value: fulfillSegmentCacheEntry( |
|
|
segmentCacheEntry, |
|
|
serverData.rsc, |
|
|
serverData.loading, |
|
|
|
|
|
|
|
|
route.staleAt, |
|
|
serverData.isPartial |
|
|
), |
|
|
|
|
|
|
|
|
closed: closed.promise, |
|
|
} |
|
|
} catch (error) { |
|
|
|
|
|
|
|
|
rejectSegmentCacheEntry(segmentCacheEntry, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
} |
|
|
|
|
|
export async function fetchSegmentPrefetchesUsingDynamicRequest( |
|
|
task: PrefetchTask, |
|
|
route: FulfilledRouteCacheEntry, |
|
|
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full, |
|
|
dynamicRequestTree: FlightRouterState, |
|
|
spawnedEntries: Map<string, PendingSegmentCacheEntry> |
|
|
): Promise<PrefetchSubtaskResult<null> | null> { |
|
|
const url = new URL(route.canonicalUrl, task.key.href) |
|
|
const nextUrl = task.key.nextUrl |
|
|
const headers: RequestHeaders = { |
|
|
[RSC_HEADER]: '1', |
|
|
[NEXT_ROUTER_STATE_TREE_HEADER]: encodeURIComponent( |
|
|
JSON.stringify(dynamicRequestTree) |
|
|
), |
|
|
} |
|
|
if (nextUrl !== null) { |
|
|
headers[NEXT_URL] = nextUrl |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fetchStrategy !== FetchStrategy.Full) { |
|
|
headers[NEXT_ROUTER_PREFETCH_HEADER] = '1' |
|
|
} |
|
|
try { |
|
|
const response = await fetchPrefetchResponse(url, headers) |
|
|
if (!response || !response.ok || !response.body) { |
|
|
|
|
|
|
|
|
rejectSegmentEntriesIfStillPending(spawnedEntries, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
|
|
|
const renderedSearch = getRenderedSearch(response) |
|
|
if (renderedSearch !== route.renderedSearch) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rejectSegmentEntriesIfStillPending(spawnedEntries, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
const closed = createPromiseWithResolvers<void>() |
|
|
|
|
|
let fulfilledEntries: Array<FulfilledSegmentCacheEntry> | null = null |
|
|
const prefetchStream = createPrefetchResponseStream( |
|
|
response.body, |
|
|
closed.resolve, |
|
|
function onResponseSizeUpdate(totalBytesReceivedSoFar) { |
|
|
|
|
|
|
|
|
|
|
|
if (fulfilledEntries === null) { |
|
|
|
|
|
|
|
|
return |
|
|
} |
|
|
const averageSize = totalBytesReceivedSoFar / fulfilledEntries.length |
|
|
for (const entry of fulfilledEntries) { |
|
|
segmentCacheLru.updateSize(entry, averageSize) |
|
|
} |
|
|
} |
|
|
) |
|
|
const serverData = await (createFromNextReadableStream( |
|
|
prefetchStream |
|
|
) as Promise<NavigationFlightResponse>) |
|
|
|
|
|
|
|
|
|
|
|
const isResponsePartial = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fulfilledEntries = writeDynamicRenderResponseIntoCache( |
|
|
Date.now(), |
|
|
task, |
|
|
fetchStrategy, |
|
|
response, |
|
|
serverData, |
|
|
isResponsePartial, |
|
|
route, |
|
|
spawnedEntries |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
return { value: null, closed: closed.promise } |
|
|
} catch (error) { |
|
|
rejectSegmentEntriesIfStillPending(spawnedEntries, Date.now() + 10 * 1000) |
|
|
return null |
|
|
} |
|
|
} |
|
|
|
|
|
function writeDynamicTreeResponseIntoCache( |
|
|
now: number, |
|
|
task: PrefetchTask, |
|
|
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full, |
|
|
response: RSCResponse, |
|
|
serverData: NavigationFlightResponse, |
|
|
entry: PendingRouteCacheEntry, |
|
|
couldBeIntercepted: boolean, |
|
|
canonicalUrl: string, |
|
|
routeIsPPREnabled: boolean |
|
|
) { |
|
|
const normalizedFlightDataResult = normalizeFlightData(serverData.f) |
|
|
if ( |
|
|
|
|
|
|
|
|
typeof normalizedFlightDataResult === 'string' || |
|
|
normalizedFlightDataResult.length !== 1 |
|
|
) { |
|
|
rejectRouteCacheEntry(entry, now + 10 * 1000) |
|
|
return |
|
|
} |
|
|
const flightData = normalizedFlightDataResult[0] |
|
|
if (!flightData.isRootRender) { |
|
|
|
|
|
rejectRouteCacheEntry(entry, now + 10 * 1000) |
|
|
return |
|
|
} |
|
|
|
|
|
const flightRouterState = flightData.tree |
|
|
|
|
|
const staleTimeHeaderSeconds = response.headers.get( |
|
|
NEXT_ROUTER_STALE_TIME_HEADER |
|
|
) |
|
|
const staleTimeMs = |
|
|
staleTimeHeaderSeconds !== null |
|
|
? parseInt(staleTimeHeaderSeconds, 10) * 1000 |
|
|
: STATIC_STALETIME_MS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isResponsePartial = |
|
|
response.headers.get(NEXT_DID_POSTPONE_HEADER) === '1' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const renderedSearch = getRenderedSearch(response) |
|
|
|
|
|
const fulfilledEntry = fulfillRouteCacheEntry( |
|
|
entry, |
|
|
convertRootFlightRouterStateToRouteTree(flightRouterState), |
|
|
flightData.head, |
|
|
flightData.isHeadPartial, |
|
|
now + staleTimeMs, |
|
|
couldBeIntercepted, |
|
|
canonicalUrl, |
|
|
renderedSearch, |
|
|
routeIsPPREnabled |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writeDynamicRenderResponseIntoCache( |
|
|
now, |
|
|
task, |
|
|
fetchStrategy, |
|
|
response, |
|
|
serverData, |
|
|
isResponsePartial, |
|
|
fulfilledEntry, |
|
|
null |
|
|
) |
|
|
} |
|
|
|
|
|
function rejectSegmentEntriesIfStillPending( |
|
|
entries: Map<string, SegmentCacheEntry>, |
|
|
staleAt: number |
|
|
): Array<FulfilledSegmentCacheEntry> { |
|
|
const fulfilledEntries = [] |
|
|
for (const entry of entries.values()) { |
|
|
if (entry.status === EntryStatus.Pending) { |
|
|
rejectSegmentCacheEntry(entry, staleAt) |
|
|
} else if (entry.status === EntryStatus.Fulfilled) { |
|
|
fulfilledEntries.push(entry) |
|
|
} |
|
|
} |
|
|
return fulfilledEntries |
|
|
} |
|
|
|
|
|
function writeDynamicRenderResponseIntoCache( |
|
|
now: number, |
|
|
task: PrefetchTask, |
|
|
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full, |
|
|
response: RSCResponse, |
|
|
serverData: NavigationFlightResponse, |
|
|
isResponsePartial: boolean, |
|
|
route: FulfilledRouteCacheEntry, |
|
|
spawnedEntries: Map<string, PendingSegmentCacheEntry> | null |
|
|
): Array<FulfilledSegmentCacheEntry> | null { |
|
|
if (serverData.b !== getAppBuildId()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (spawnedEntries !== null) { |
|
|
rejectSegmentEntriesIfStillPending(spawnedEntries, now + 10 * 1000) |
|
|
} |
|
|
return null |
|
|
} |
|
|
const flightDatas = normalizeFlightData(serverData.f) |
|
|
if (typeof flightDatas === 'string') { |
|
|
|
|
|
|
|
|
return null |
|
|
} |
|
|
|
|
|
const staleTimeHeaderSeconds = response.headers.get( |
|
|
NEXT_ROUTER_STALE_TIME_HEADER |
|
|
) |
|
|
const staleTimeMs = |
|
|
staleTimeHeaderSeconds !== null |
|
|
? parseInt(staleTimeHeaderSeconds, 10) * 1000 |
|
|
: STATIC_STALETIME_MS |
|
|
const staleAt = now + staleTimeMs |
|
|
|
|
|
for (const flightData of flightDatas) { |
|
|
const seedData = flightData.seedData |
|
|
if (seedData !== null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const segmentPath = flightData.segmentPath |
|
|
let segmentKey = ROOT_SEGMENT_KEY |
|
|
for (let i = 0; i < segmentPath.length; i += 2) { |
|
|
const parallelRouteKey: string = segmentPath[i] |
|
|
const segment: FlightRouterStateSegment = segmentPath[i + 1] |
|
|
segmentKey = encodeChildSegmentKey( |
|
|
segmentKey, |
|
|
parallelRouteKey, |
|
|
encodeSegment(segment) |
|
|
) |
|
|
} |
|
|
|
|
|
writeSeedDataIntoCache( |
|
|
now, |
|
|
task, |
|
|
fetchStrategy, |
|
|
route, |
|
|
staleAt, |
|
|
seedData, |
|
|
isResponsePartial, |
|
|
segmentKey, |
|
|
spawnedEntries |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
route.head = flightData.head |
|
|
route.isHeadPartial = flightData.isHeadPartial |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (staleAt < route.staleAt) { |
|
|
route.staleAt = staleAt |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (spawnedEntries !== null) { |
|
|
const fulfilledEntries = rejectSegmentEntriesIfStillPending( |
|
|
spawnedEntries, |
|
|
now + 10 * 1000 |
|
|
) |
|
|
return fulfilledEntries |
|
|
} |
|
|
return null |
|
|
} |
|
|
|
|
|
function writeSeedDataIntoCache( |
|
|
now: number, |
|
|
task: PrefetchTask, |
|
|
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full, |
|
|
route: FulfilledRouteCacheEntry, |
|
|
staleAt: number, |
|
|
seedData: CacheNodeSeedData, |
|
|
isResponsePartial: boolean, |
|
|
key: string, |
|
|
entriesOwnedByCurrentTask: Map<string, PendingSegmentCacheEntry> | null |
|
|
) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rsc = seedData[1] |
|
|
const loading = seedData[3] |
|
|
const isPartial = rsc === null || isResponsePartial |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ownedEntry = |
|
|
entriesOwnedByCurrentTask !== null |
|
|
? entriesOwnedByCurrentTask.get(key) |
|
|
: undefined |
|
|
if (ownedEntry !== undefined) { |
|
|
fulfillSegmentCacheEntry(ownedEntry, rsc, loading, staleAt, isPartial) |
|
|
} else { |
|
|
|
|
|
const possiblyNewEntry = readOrCreateSegmentCacheEntry( |
|
|
now, |
|
|
task, |
|
|
route, |
|
|
key |
|
|
) |
|
|
if (possiblyNewEntry.status === EntryStatus.Empty) { |
|
|
|
|
|
const newEntry = possiblyNewEntry |
|
|
fulfillSegmentCacheEntry( |
|
|
upgradeToPendingSegment(newEntry, fetchStrategy), |
|
|
rsc, |
|
|
loading, |
|
|
staleAt, |
|
|
isPartial |
|
|
) |
|
|
} else { |
|
|
|
|
|
|
|
|
const newEntry = fulfillSegmentCacheEntry( |
|
|
upgradeToPendingSegment( |
|
|
createDetachedSegmentCacheEntry(staleAt), |
|
|
fetchStrategy |
|
|
), |
|
|
rsc, |
|
|
loading, |
|
|
staleAt, |
|
|
isPartial |
|
|
) |
|
|
upsertSegmentEntry( |
|
|
now, |
|
|
getSegmentKeypathForTask(task, route, key), |
|
|
newEntry |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
const seedDataChildren = seedData[2] |
|
|
if (seedDataChildren !== null) { |
|
|
for (const parallelRouteKey in seedDataChildren) { |
|
|
const childSeedData = seedDataChildren[parallelRouteKey] |
|
|
if (childSeedData !== null) { |
|
|
const childSegment = childSeedData[0] |
|
|
writeSeedDataIntoCache( |
|
|
now, |
|
|
task, |
|
|
fetchStrategy, |
|
|
route, |
|
|
staleAt, |
|
|
childSeedData, |
|
|
isResponsePartial, |
|
|
encodeChildSegmentKey( |
|
|
key, |
|
|
parallelRouteKey, |
|
|
encodeSegment(childSegment) |
|
|
), |
|
|
entriesOwnedByCurrentTask |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
async function fetchPrefetchResponse( |
|
|
url: URL, |
|
|
headers: RequestHeaders |
|
|
): Promise<RSCResponse | null> { |
|
|
const fetchPriority = 'low' |
|
|
const response = await createFetch(url, headers, fetchPriority) |
|
|
if (!response.ok) { |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
if (isOutputExportMode) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
const contentType = response.headers.get('content-type') |
|
|
const isFlightResponse = |
|
|
contentType && contentType.startsWith(RSC_CONTENT_TYPE_HEADER) |
|
|
if (!isFlightResponse) { |
|
|
return null |
|
|
} |
|
|
} |
|
|
return response |
|
|
} |
|
|
|
|
|
function createPrefetchResponseStream( |
|
|
originalFlightStream: ReadableStream<Uint8Array>, |
|
|
onStreamClose: () => void, |
|
|
onResponseSizeUpdate: (size: number) => void |
|
|
): ReadableStream<Uint8Array> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let totalByteLength = 0 |
|
|
const reader = originalFlightStream.getReader() |
|
|
return new ReadableStream({ |
|
|
async pull(controller) { |
|
|
while (true) { |
|
|
const { done, value } = await reader.read() |
|
|
if (!done) { |
|
|
|
|
|
|
|
|
controller.enqueue(value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
totalByteLength += value.byteLength |
|
|
onResponseSizeUpdate(totalByteLength) |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
onStreamClose() |
|
|
return |
|
|
} |
|
|
}, |
|
|
}) |
|
|
} |
|
|
|
|
|
function addSegmentPathToUrlInOutputExportMode( |
|
|
url: URL, |
|
|
segmentPath: string |
|
|
): URL { |
|
|
if (isOutputExportMode) { |
|
|
|
|
|
|
|
|
const staticUrl = new URL(url) |
|
|
const routeDir = staticUrl.pathname.endsWith('/') |
|
|
? staticUrl.pathname.substring(0, -1) |
|
|
: staticUrl.pathname |
|
|
const staticExportFilename = |
|
|
convertSegmentPathToStaticExportFilename(segmentPath) |
|
|
staticUrl.pathname = `${routeDir}/${staticExportFilename}` |
|
|
return staticUrl |
|
|
} |
|
|
return url |
|
|
} |
|
|
|
|
|
function createPromiseWithResolvers<T>(): PromiseWithResolvers<T> { |
|
|
|
|
|
let resolve: (value: T | PromiseLike<T>) => void |
|
|
let reject: (reason: any) => void |
|
|
const promise = new Promise<T>((res, rej) => { |
|
|
resolve = res |
|
|
reject = rej |
|
|
}) |
|
|
return { resolve: resolve!, reject: reject!, promise } |
|
|
} |
|
|
|