Commit 02bd4b5c authored by Andrew Clark's avatar Andrew Clark Committed by Jiachi Liu

Split each path param into a separate cache key (#85758)

Refactors the keypath used to cache segments so that each route param is
a separate cache key. The search string was already its own separate
key, so this generalizes it to include path params, too.

This is a prerequisite for reusing cache entries across different values
of a param if the server indicates that the response does not vary on
that param. Currently the server does not send that information, but
we'll add this in a subsequent step.

As part of this change, this refines the concept of a "keypath" to be
called a "vary path" instead. From the perspective of the CacheMap
module, they are functionally identical, but from the perspective of the
overall caching design, it's more specific: a vary path is a pure
function of a segment's position in a particular route tree and the
(post-rewrite) URL that is being queried. More concretely, successive
queries of the cache for the same segment always use the same vary path.
parent fd8131f0
...@@ -7,7 +7,7 @@ import { deleteFromCacheMap } from './cache-map' ...@@ -7,7 +7,7 @@ import { deleteFromCacheMap } from './cache-map'
// The MapEntry type is used as an LRU node, too. We choose this one instead of // The MapEntry type is used as an LRU node, too. We choose this one instead of
// the inner cache entry type (RouteCacheEntry, SegmentCacheEntry) because it's // the inner cache entry type (RouteCacheEntry, SegmentCacheEntry) because it's
// monomorphic and can be optimized by the VM. // monomorphic and can be optimized by the VM.
type LRUNode = MapEntry<any, any> type LRUNode = MapEntry<any>
let head: LRUNode | null = null let head: LRUNode | null = null
let didScheduleCleanup: boolean = false let didScheduleCleanup: boolean = false
......
...@@ -19,7 +19,6 @@ import { createHrefFromUrl } from '../router-reducer/create-href-from-url' ...@@ -19,7 +19,6 @@ import { createHrefFromUrl } from '../router-reducer/create-href-from-url'
import { import {
EntryStatus, EntryStatus,
readRouteCacheEntry, readRouteCacheEntry,
getCanonicalSegmentKeypath,
readSegmentCacheEntry, readSegmentCacheEntry,
waitForSegmentCacheEntry, waitForSegmentCacheEntry,
requestOptimisticRouteCacheEntry, requestOptimisticRouteCacheEntry,
...@@ -347,11 +346,7 @@ function readRenderSnapshotFromCache( ...@@ -347,11 +346,7 @@ function readRenderSnapshotFromCache(
let loading: LoadingModuleData | Promise<LoadingModuleData> = null let loading: LoadingModuleData | Promise<LoadingModuleData> = null
let isPartial: boolean = true let isPartial: boolean = true
const canonicalSegmentKeypath = getCanonicalSegmentKeypath( const segmentEntry = readSegmentCacheEntry(now, tree.varyPath)
route,
tree.cacheKey
)
const segmentEntry = readSegmentCacheEntry(now, canonicalSegmentKeypath)
if (segmentEntry !== null) { if (segmentEntry !== null) {
switch (segmentEntry.status) { switch (segmentEntry.status) {
case EntryStatus.Fulfilled: { case EntryStatus.Fulfilled: {
...@@ -423,11 +418,7 @@ function readHeadSnapshotFromCache( ...@@ -423,11 +418,7 @@ function readHeadSnapshotFromCache(
// Same as readRenderSnapshotFromCache, but for the head // Same as readRenderSnapshotFromCache, but for the head
let rsc: React.ReactNode | null = null let rsc: React.ReactNode | null = null
let isPartial: boolean = true let isPartial: boolean = true
const canonicalSegmentKeypath = getCanonicalSegmentKeypath( const segmentEntry = readSegmentCacheEntry(now, route.metadata.varyPath)
route,
route.metadata.cacheKey
)
const segmentEntry = readSegmentCacheEntry(now, canonicalSegmentKeypath)
if (segmentEntry !== null) { if (segmentEntry !== null) {
switch (segmentEntry.status) { switch (segmentEntry.status) {
case EntryStatus.Fulfilled: { case EntryStatus.Fulfilled: {
......
...@@ -24,10 +24,9 @@ import { ...@@ -24,10 +24,9 @@ import {
upgradeToPendingSegment, upgradeToPendingSegment,
waitForSegmentCacheEntry, waitForSegmentCacheEntry,
overwriteRevalidatingSegmentCacheEntry, overwriteRevalidatingSegmentCacheEntry,
getGenericSegmentKeypathFromFetchStrategy,
canNewFetchStrategyProvideMoreContent, canNewFetchStrategyProvideMoreContent,
type SegmentCacheKeypath,
} from './cache' } from './cache'
import { getSegmentVaryPathForRequest, type SegmentVaryPath } from './vary-path'
import type { RouteCacheKey } from './cache-key' import type { RouteCacheKey } from './cache-key'
import { createCacheKey } from './cache-key' import { createCacheKey } from './cache-key'
import { import {
...@@ -40,7 +39,7 @@ import { ...@@ -40,7 +39,7 @@ import {
addSearchParamsIfPageSegment, addSearchParamsIfPageSegment,
PAGE_SEGMENT_KEY, PAGE_SEGMENT_KEY,
} from '../../../shared/lib/segment' } from '../../../shared/lib/segment'
import type { SegmentCacheKey } from '../../../shared/lib/segment-cache/segment-value-encoding' import type { SegmentRequestKey } from '../../../shared/lib/segment-cache/segment-value-encoding'
const scheduleMicrotask = const scheduleMicrotask =
typeof queueMicrotask === 'function' typeof queueMicrotask === 'function'
...@@ -122,7 +121,7 @@ export type PrefetchTask = { ...@@ -122,7 +121,7 @@ export type PrefetchTask = {
* They are reset after each iteration of the task queue. * They are reset after each iteration of the task queue.
*/ */
hasBackgroundWork: boolean hasBackgroundWork: boolean
spawnedRuntimePrefetches: Set<SegmentCacheKey> | null spawnedRuntimePrefetches: Set<SegmentRequestKey> | null
/** /**
* True if the prefetch was cancelled. * True if the prefetch was cancelled.
...@@ -691,7 +690,7 @@ function pingRootRouteTree( ...@@ -691,7 +690,7 @@ function pingRootRouteTree(
// During the first pass, we discovered segments that require a // During the first pass, we discovered segments that require a
// runtime prefetch. Do a second pass to construct a request tree. // runtime prefetch. Do a second pass to construct a request tree.
const spawnedEntries = new Map< const spawnedEntries = new Map<
SegmentCacheKey, SegmentRequestKey,
PendingSegmentCacheEntry PendingSegmentCacheEntry
>() >()
pingRuntimeHead( pingRuntimeHead(
...@@ -737,7 +736,7 @@ function pingRootRouteTree( ...@@ -737,7 +736,7 @@ function pingRootRouteTree(
// really that much duplication, just would be nice to remove one of // really that much duplication, just would be nice to remove one of
// these codepaths. // these codepaths.
const spawnedEntries = new Map< const spawnedEntries = new Map<
SegmentCacheKey, SegmentRequestKey,
PendingSegmentCacheEntry PendingSegmentCacheEntry
>() >()
pingRuntimeHead(now, task, route, spawnedEntries, fetchStrategy) pingRuntimeHead(now, task, route, spawnedEntries, fetchStrategy)
...@@ -792,7 +791,7 @@ function pingStaticHead( ...@@ -792,7 +791,7 @@ function pingStaticHead(
now, now,
FetchStrategy.PPR, FetchStrategy.PPR,
route, route,
route.metadata.cacheKey route.metadata
), ),
task.key, task.key,
route.metadata route.metadata
...@@ -803,7 +802,7 @@ function pingRuntimeHead( ...@@ -803,7 +802,7 @@ function pingRuntimeHead(
now: number, now: number,
task: PrefetchTask, task: PrefetchTask,
route: FulfilledRouteCacheEntry, route: FulfilledRouteCacheEntry,
spawnedEntries: Map<string, PendingSegmentCacheEntry>, spawnedEntries: Map<SegmentRequestKey, PendingSegmentCacheEntry>,
fetchStrategy: fetchStrategy:
| FetchStrategy.Full | FetchStrategy.Full
| FetchStrategy.PPRRuntime | FetchStrategy.PPRRuntime
...@@ -849,7 +848,7 @@ function pingSharedPartOfCacheComponentsTree( ...@@ -849,7 +848,7 @@ function pingSharedPartOfCacheComponentsTree(
now, now,
task.fetchStrategy, task.fetchStrategy,
route, route,
newTree.cacheKey newTree
) )
pingStaticSegmentData(now, task, route, segment, task.key, newTree) pingStaticSegmentData(now, task, route, segment, task.key, newTree)
...@@ -937,9 +936,9 @@ function pingNewPartOfCacheComponentsTree( ...@@ -937,9 +936,9 @@ function pingNewPartOfCacheComponentsTree(
// This will signal to the outer task queue that a second traversal is // This will signal to the outer task queue that a second traversal is
// required to construct a request tree. // required to construct a request tree.
if (task.spawnedRuntimePrefetches === null) { if (task.spawnedRuntimePrefetches === null) {
task.spawnedRuntimePrefetches = new Set([tree.cacheKey]) task.spawnedRuntimePrefetches = new Set([tree.requestKey])
} else { } else {
task.spawnedRuntimePrefetches.add(tree.cacheKey) task.spawnedRuntimePrefetches.add(tree.requestKey)
} }
// Then exit the traversal without prefetching anything further. // Then exit the traversal without prefetching anything further.
return PrefetchTaskExitStatus.Done return PrefetchTaskExitStatus.Done
...@@ -950,7 +949,7 @@ function pingNewPartOfCacheComponentsTree( ...@@ -950,7 +949,7 @@ function pingNewPartOfCacheComponentsTree(
now, now,
task.fetchStrategy, task.fetchStrategy,
route, route,
tree.cacheKey tree
) )
pingStaticSegmentData(now, task, route, segment, task.key, tree) pingStaticSegmentData(now, task, route, segment, task.key, tree)
if (tree.slots !== null) { if (tree.slots !== null) {
...@@ -983,7 +982,7 @@ function diffRouteTreeAgainstCurrent( ...@@ -983,7 +982,7 @@ function diffRouteTreeAgainstCurrent(
route: FulfilledRouteCacheEntry, route: FulfilledRouteCacheEntry,
oldTree: FlightRouterState, oldTree: FlightRouterState,
newTree: RouteTree, newTree: RouteTree,
spawnedEntries: Map<string, PendingSegmentCacheEntry>, spawnedEntries: Map<SegmentRequestKey, PendingSegmentCacheEntry>,
fetchStrategy: fetchStrategy:
| FetchStrategy.Full | FetchStrategy.Full
| FetchStrategy.PPRRuntime | FetchStrategy.PPRRuntime
...@@ -1130,7 +1129,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary( ...@@ -1130,7 +1129,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
route: FulfilledRouteCacheEntry, route: FulfilledRouteCacheEntry,
tree: RouteTree, tree: RouteTree,
refetchMarkerContext: 'refetch' | 'inside-shared-layout' | null, refetchMarkerContext: 'refetch' | 'inside-shared-layout' | null,
spawnedEntries: Map<string, PendingSegmentCacheEntry> spawnedEntries: Map<SegmentRequestKey, PendingSegmentCacheEntry>
): FlightRouterState { ): FlightRouterState {
// This function is similar to pingRouteTreeAndIncludeDynamicData, except the // This function is similar to pingRouteTreeAndIncludeDynamicData, except the
// server is only going to return a minimal loading state — it will stop // server is only going to return a minimal loading state — it will stop
...@@ -1150,7 +1149,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary( ...@@ -1150,7 +1149,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
now, now,
task.fetchStrategy, task.fetchStrategy,
route, route,
tree.cacheKey tree
) )
switch (segment.status) { switch (segment.status) {
case EntryStatus.Empty: { case EntryStatus.Empty: {
...@@ -1164,7 +1163,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary( ...@@ -1164,7 +1163,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
// Add the pending cache entry to the result map. // Add the pending cache entry to the result map.
spawnedEntries.set( spawnedEntries.set(
tree.cacheKey, tree.requestKey,
upgradeToPendingSegment( upgradeToPendingSegment(
segment, segment,
// Set the fetch strategy to LoadingBoundary to indicate that the server // Set the fetch strategy to LoadingBoundary to indicate that the server
...@@ -1243,7 +1242,7 @@ function pingRouteTreeAndIncludeDynamicData( ...@@ -1243,7 +1242,7 @@ function pingRouteTreeAndIncludeDynamicData(
route: FulfilledRouteCacheEntry, route: FulfilledRouteCacheEntry,
tree: RouteTree, tree: RouteTree,
isInsideRefetchingParent: boolean, isInsideRefetchingParent: boolean,
spawnedEntries: Map<string, PendingSegmentCacheEntry>, spawnedEntries: Map<SegmentRequestKey, PendingSegmentCacheEntry>,
fetchStrategy: FetchStrategy.Full | FetchStrategy.PPRRuntime fetchStrategy: FetchStrategy.Full | FetchStrategy.PPRRuntime
): FlightRouterState { ): FlightRouterState {
// The tree we're constructing is the same shape as the tree we're navigating // The tree we're constructing is the same shape as the tree we're navigating
...@@ -1263,7 +1262,7 @@ function pingRouteTreeAndIncludeDynamicData( ...@@ -1263,7 +1262,7 @@ function pingRouteTreeAndIncludeDynamicData(
// entries that include search params. // entries that include search params.
fetchStrategy, fetchStrategy,
route, route,
tree.cacheKey tree
) )
let spawnedSegment: PendingSegmentCacheEntry | null = null let spawnedSegment: PendingSegmentCacheEntry | null = null
...@@ -1338,7 +1337,7 @@ function pingRouteTreeAndIncludeDynamicData( ...@@ -1338,7 +1337,7 @@ function pingRouteTreeAndIncludeDynamicData(
if (spawnedSegment !== null) { if (spawnedSegment !== null) {
// Add the pending entry to the result map. // Add the pending entry to the result map.
spawnedEntries.set(tree.cacheKey, spawnedSegment) spawnedEntries.set(tree.requestKey, spawnedSegment)
} }
// Don't bother to add a refetch marker if one is already present in a parent. // Don't bother to add a refetch marker if one is already present in a parent.
...@@ -1360,8 +1359,8 @@ function pingRuntimePrefetches( ...@@ -1360,8 +1359,8 @@ function pingRuntimePrefetches(
task: PrefetchTask, task: PrefetchTask,
route: FulfilledRouteCacheEntry, route: FulfilledRouteCacheEntry,
tree: RouteTree, tree: RouteTree,
spawnedRuntimePrefetches: Set<SegmentCacheKey>, spawnedRuntimePrefetches: Set<SegmentRequestKey>,
spawnedEntries: Map<string, PendingSegmentCacheEntry> spawnedEntries: Map<SegmentRequestKey, PendingSegmentCacheEntry>
): FlightRouterState { ): FlightRouterState {
// Construct a request tree (FlightRouterState) for a runtime prefetch. If // Construct a request tree (FlightRouterState) for a runtime prefetch. If
// a segment is part of the runtime prefetch, the tree is constructed by // a segment is part of the runtime prefetch, the tree is constructed by
...@@ -1369,7 +1368,7 @@ function pingRuntimePrefetches( ...@@ -1369,7 +1368,7 @@ function pingRuntimePrefetches(
// a regular FlightRouterState with no special markers. // a regular FlightRouterState with no special markers.
// //
// See pingRouteTreeAndIncludeDynamicData for details. // See pingRouteTreeAndIncludeDynamicData for details.
if (spawnedRuntimePrefetches.has(tree.cacheKey)) { if (spawnedRuntimePrefetches.has(tree.requestKey)) {
// This segment needs a runtime prefetch. // This segment needs a runtime prefetch.
return pingRouteTreeAndIncludeDynamicData( return pingRouteTreeAndIncludeDynamicData(
now, now,
...@@ -1502,7 +1501,7 @@ function pingPPRSegmentRevalidation( ...@@ -1502,7 +1501,7 @@ function pingPPRSegmentRevalidation(
now, now,
FetchStrategy.PPR, FetchStrategy.PPR,
route, route,
tree.cacheKey tree
) )
switch (revalidatingSegment.status) { switch (revalidatingSegment.status) {
case EntryStatus.Empty: case EntryStatus.Empty:
...@@ -1517,11 +1516,7 @@ function pingPPRSegmentRevalidation( ...@@ -1517,11 +1516,7 @@ function pingPPRSegmentRevalidation(
tree tree
) )
), ),
getGenericSegmentKeypathFromFetchStrategy( getSegmentVaryPathForRequest(FetchStrategy.PPR, tree)
FetchStrategy.PPR,
route,
tree.cacheKey
)
) )
break break
case EntryStatus.Pending: case EntryStatus.Pending:
...@@ -1548,7 +1543,7 @@ function pingFullSegmentRevalidation( ...@@ -1548,7 +1543,7 @@ function pingFullSegmentRevalidation(
now, now,
fetchStrategy, fetchStrategy,
route, route,
tree.cacheKey tree
) )
if (revalidatingSegment.status === EntryStatus.Empty) { if (revalidatingSegment.status === EntryStatus.Empty) {
// During a Full/PPRRuntime prefetch, a single dynamic request is made for all the // During a Full/PPRRuntime prefetch, a single dynamic request is made for all the
...@@ -1562,11 +1557,7 @@ function pingFullSegmentRevalidation( ...@@ -1562,11 +1557,7 @@ function pingFullSegmentRevalidation(
) )
upsertSegmentOnCompletion( upsertSegmentOnCompletion(
waitForSegmentCacheEntry(pendingSegment), waitForSegmentCacheEntry(pendingSegment),
getGenericSegmentKeypathFromFetchStrategy( getSegmentVaryPathForRequest(fetchStrategy, tree)
fetchStrategy,
route,
tree.cacheKey
)
) )
return pendingSegment return pendingSegment
} else { } else {
...@@ -1583,7 +1574,7 @@ function pingFullSegmentRevalidation( ...@@ -1583,7 +1574,7 @@ function pingFullSegmentRevalidation(
const emptySegment = overwriteRevalidatingSegmentCacheEntry( const emptySegment = overwriteRevalidatingSegmentCacheEntry(
fetchStrategy, fetchStrategy,
route, route,
tree.cacheKey tree
) )
const pendingSegment = upgradeToPendingSegment( const pendingSegment = upgradeToPendingSegment(
emptySegment, emptySegment,
...@@ -1591,11 +1582,7 @@ function pingFullSegmentRevalidation( ...@@ -1591,11 +1582,7 @@ function pingFullSegmentRevalidation(
) )
upsertSegmentOnCompletion( upsertSegmentOnCompletion(
waitForSegmentCacheEntry(pendingSegment), waitForSegmentCacheEntry(pendingSegment),
getGenericSegmentKeypathFromFetchStrategy( getSegmentVaryPathForRequest(fetchStrategy, tree)
fetchStrategy,
route,
tree.cacheKey
)
) )
return pendingSegment return pendingSegment
} }
...@@ -1620,13 +1607,13 @@ const noop = () => {} ...@@ -1620,13 +1607,13 @@ const noop = () => {}
function upsertSegmentOnCompletion( function upsertSegmentOnCompletion(
promise: Promise<FulfilledSegmentCacheEntry | null>, promise: Promise<FulfilledSegmentCacheEntry | null>,
keypath: SegmentCacheKeypath varyPath: SegmentVaryPath
) { ) {
// Wait for a segment to finish loading, then upsert it into the cache // Wait for a segment to finish loading, then upsert it into the cache
promise.then((fulfilled) => { promise.then((fulfilled) => {
if (fulfilled !== null) { if (fulfilled !== null) {
// Received new data. Attempt to replace the existing entry in the cache. // Received new data. Attempt to replace the existing entry in the cache.
upsertSegmentEntry(Date.now(), keypath, fulfilled) upsertSegmentEntry(Date.now(), varyPath, fulfilled)
} }
}, noop) }, noop)
} }
......
import { FetchStrategy } from './types'
import type {
NormalizedPathname,
NormalizedSearch,
NormalizedNextUrl,
} from './cache-key'
import type { RouteTree } from './cache'
import { Fallback, type FallbackType } from './cache-map'
import { HEAD_REQUEST_KEY } from '../../../shared/lib/segment-cache/segment-value-encoding'
type Opaque<T, K> = T & { __brand: K }
/**
* A linked-list of all the params (or other param-like) inputs that a cache
* entry may vary by. This is used by the CacheMap module to reuse cache entries
* across different param values. If a param has a value of Fallback, it means
* the cache entry is reusable for all possible values of that param. See
* cache-map.ts for details.
*
* A segment's vary path is a pure function of a segment's position in a
* particular route tree and the (post-rewrite) URL that is being queried. More
* concretely, successive queries of the cache for the same segment always use
* the same vary path.
*
* A route's vary path is simpler: it's comprised of the pathname, search
* string, and Next-URL header.
*/
export type VaryPath = {
value: string | null | FallbackType
parent: VaryPath | null
}
// Because it's so important for vary paths to line up across cache accesses,
// we use opaque type aliases to ensure these are only created within
// this module.
// requestKey -> searchParams -> nextUrl
export type RouteVaryPath = Opaque<
{
value: NormalizedPathname
parent: {
value: NormalizedSearch
parent: {
value: NormalizedNextUrl | null | FallbackType
parent: null
}
}
},
'RouteVaryPath'
>
// requestKey -> pathParams
export type LayoutVaryPath = Opaque<
{
value: string
parent: PartialSegmentVaryPath | null
},
'LayoutVaryPath'
>
// requestKey -> searchParams -> pathParams
export type PageVaryPath = Opaque<
{
value: string
parent: {
value: NormalizedSearch | FallbackType
parent: PartialSegmentVaryPath | null
}
},
'PageVaryPath'
>
export type SegmentVaryPath = LayoutVaryPath | PageVaryPath
// Intermediate type used when building a vary path during a recursive traversal
// of the route tree.
export type PartialSegmentVaryPath = Opaque<VaryPath, 'PartialSegmentVaryPath'>
export function getRouteVaryPath(
pathname: NormalizedPathname,
search: NormalizedSearch,
nextUrl: NormalizedNextUrl | null
): RouteVaryPath {
// requestKey -> searchParams -> nextUrl
const varyPath: VaryPath = {
value: pathname,
parent: {
value: search,
parent: {
value: nextUrl,
parent: null,
},
},
}
return varyPath as RouteVaryPath
}
export function getFulfilledRouteVaryPath(
pathname: NormalizedPathname,
search: NormalizedSearch,
nextUrl: NormalizedNextUrl | null,
couldBeIntercepted: boolean
): RouteVaryPath {
// This is called when a route's data is fulfilled. The cache entry will be
// re-keyed based on which inputs the response varies by.
// requestKey -> searchParams -> nextUrl
const varyPath: VaryPath = {
value: pathname,
parent: {
value: search,
parent: {
value: couldBeIntercepted ? nextUrl : Fallback,
parent: null,
},
},
}
return varyPath as RouteVaryPath
}
export function appendLayoutVaryPath(
parentPath: PartialSegmentVaryPath | null,
cacheKey: string
): PartialSegmentVaryPath {
const varyPathPart: VaryPath = {
value: cacheKey,
parent: parentPath,
}
return varyPathPart as PartialSegmentVaryPath
}
export function finalizeLayoutVaryPath(
requestKey: string,
varyPath: PartialSegmentVaryPath | null
): LayoutVaryPath {
const layoutVaryPath: VaryPath = {
value: requestKey,
parent: varyPath,
}
return layoutVaryPath as LayoutVaryPath
}
export function finalizePageVaryPath(
requestKey: string,
renderedSearch: NormalizedSearch,
varyPath: PartialSegmentVaryPath | null
): PageVaryPath {
// Unlike layouts, a page segment's vary path also includes the search string.
// requestKey -> searchParams -> pathParams
const pageVaryPath: VaryPath = {
value: requestKey,
parent: {
value: renderedSearch,
parent: varyPath,
},
}
return pageVaryPath as PageVaryPath
}
export function finalizeMetadataVaryPath(
pageRequestKey: string,
renderedSearch: NormalizedSearch,
varyPath: PartialSegmentVaryPath | null
): PageVaryPath {
// The metadata "segment" is not a real segment because it doesn't exist in
// the normal structure of the route tree, but in terms of caching, it
// behaves like a page segment because it varies by all the same params as
// a page.
//
// To keep the protocol for querying the server simple, the request key for
// the metadata does not include any path information. It's unnecessary from
// the server's perspective, because unlike page segments, there's only one
// metadata response per URL, i.e. there's no need to distinguish multiple
// parallel pages.
//
// However, this means the metadata request key is insufficient for
// caching the the metadata in the client cache, because on the client we
// use the request key to distinguish the metadata entry from all other
// page's metadata entries.
//
// So instead we create a simulated request key based on the page segment.
// Conceptually this is equivalent to the request key the server would have
// assigned the metadata segment if it treated it as part of the actual
// route structure.
// If there are multiple parallel pages, we use whichever is the first one.
// This is fine because the only difference between request keys for
// different parallel pages are things like route groups and parallel
// route slots. As long as it's always the same one, it doesn't matter.
const pageVaryPath: VaryPath = {
// Append the actual metadata request key to the page request key. Note
// that we're not using a separate vary path part; it's unnecessary because
// these are not conceptually separate inputs.
value: pageRequestKey + HEAD_REQUEST_KEY,
parent: {
value: renderedSearch,
parent: varyPath,
},
}
return pageVaryPath as PageVaryPath
}
export function getSegmentVaryPathForRequest(
fetchStrategy: FetchStrategy,
tree: RouteTree
): SegmentVaryPath {
// This is used for storing pending requests in the cache. We want to choose
// the most generic vary path based on the strategy used to fetch it, i.e.
// static/PPR versus runtime prefetching, so that it can be reused as much
// as possible.
//
// We may be able to re-key the response to something even more generic once
// we receive it — for example, if the server tells us that the response
// doesn't vary on a particular param — but even before we send the request,
// we know some params are reusable based on the fetch strategy alone. For
// example, a static prefetch will never vary on search params.
//
// The original vary path with all the params filled in is stored on the
// route tree object. We will clone this one to create a new vary path
// where certain params are replaced with Fallback.
//
// This result of this function is not stored anywhere. It's only used to
// access the cache a single time.
//
// TODO: Rather than create a new list object just to access the cache, the
// plan is to add the concept of a "vary mask". This will represent all the
// params that can be treated as Fallback. (Or perhaps the inverse.)
const originalVaryPath = tree.varyPath
// Only page segments (and the special "metadata" segment, which is treated
// like a page segment for the purposes of caching) may contain search
// params. There's no reason to include them in the vary path otherwise.
if (tree.isPage) {
// Only a runtime prefetch will include search params in the vary path.
// Static prefetches never include search params, so they can be reused
// across all possible search param values.
const doesVaryOnSearchParams =
fetchStrategy === FetchStrategy.Full ||
fetchStrategy === FetchStrategy.PPRRuntime
if (!doesVaryOnSearchParams) {
// The response from the the server will not vary on search params. Clone
// the end of the original vary path to replace the search params
// with Fallback.
//
// requestKey -> searchParams -> pathParams
// ^ This part gets replaced with Fallback
const searchParamsVaryPath = (originalVaryPath as PageVaryPath).parent
const pathParamsVaryPath = searchParamsVaryPath.parent
const patchedVaryPath: VaryPath = {
value: originalVaryPath.value,
parent: {
value: Fallback,
parent: pathParamsVaryPath,
},
}
return patchedVaryPath as SegmentVaryPath
}
}
// The request does vary on search params. We don't need to modify anything.
return originalVaryPath as SegmentVaryPath
}
export function clonePageVaryPathWithNewSearchParams(
originalVaryPath: PageVaryPath,
newSearch: NormalizedSearch
): PageVaryPath {
// requestKey -> searchParams -> pathParams
// ^ This part gets replaced with newSearch
const searchParamsVaryPath = originalVaryPath.parent
const clonedVaryPath: VaryPath = {
value: originalVaryPath.value,
parent: {
value: newSearch,
parent: searchParamsVaryPath.parent,
},
}
return clonedVaryPath as PageVaryPath
}
...@@ -19,12 +19,6 @@ import type { ParsedUrlQuery } from 'querystring' ...@@ -19,12 +19,6 @@ import type { ParsedUrlQuery } from 'querystring'
export type RouteParamValue = string | Array<string> | null export type RouteParamValue = string | Array<string> | null
export type RouteParam = {
name: string
value: RouteParamValue
type: DynamicParamTypesShort
}
export function getRenderedSearch( export function getRenderedSearch(
response: RSCResponse<unknown> | Response response: RSCResponse<unknown> | Response
): NormalizedSearch { ): NormalizedSearch {
......
import { PAGE_SEGMENT_KEY } from '../segment' import { PAGE_SEGMENT_KEY } from '../segment'
import type { Segment as FlightRouterStateSegment } from '../app-router-types' import type { Segment as FlightRouterStateSegment } from '../app-router-types'
import type { NormalizedPathname } from '../../../client/components/segment-cache/cache-key'
// TypeScript trick to simulate opaque types, like in Flow. // TypeScript trick to simulate opaque types, like in Flow.
type Opaque<K, T> = T & { __brand: K } type Opaque<K, T> = T & { __brand: K }
export type SegmentRequestKeyPart = Opaque<'SegmentRequestKeyPart', string> export type SegmentRequestKeyPart = Opaque<'SegmentRequestKeyPart', string>
export type SegmentRequestKey = Opaque<'SegmentRequestKey', string> export type SegmentRequestKey = Opaque<'SegmentRequestKey', string>
export type SegmentCacheKeyPart = Opaque<'SegmentCacheKeyPart', string>
export type SegmentCacheKey = Opaque<'SegmentCacheKey', string>
export const ROOT_SEGMENT_REQUEST_KEY = '' as SegmentRequestKey export const ROOT_SEGMENT_REQUEST_KEY = '' as SegmentRequestKey
export const ROOT_SEGMENT_CACHE_KEY = '' as SegmentCacheKey
export const HEAD_REQUEST_KEY = '/_head' as SegmentRequestKey export const HEAD_REQUEST_KEY = '/_head' as SegmentRequestKey
...@@ -71,48 +67,6 @@ export function appendSegmentRequestKeyPart( ...@@ -71,48 +67,6 @@ export function appendSegmentRequestKeyPart(
return (parentRequestKey + '/' + slotKey) as SegmentRequestKey return (parentRequestKey + '/' + slotKey) as SegmentRequestKey
} }
export function createSegmentCacheKeyPart(
requestKeyPart: SegmentRequestKeyPart,
segment: FlightRouterStateSegment
): SegmentCacheKeyPart {
if (typeof segment === 'string') {
return requestKeyPart as any as SegmentCacheKeyPart
}
const paramValue = segment[1]
const safeValue = encodeToFilesystemAndURLSafeString(paramValue)
return (requestKeyPart + '$' + safeValue) as SegmentCacheKeyPart
}
export function appendSegmentCacheKeyPart(
parentSegmentKey: SegmentCacheKey,
parallelRouteKey: string,
childCacheKeyPart: SegmentCacheKeyPart
): SegmentCacheKey {
const slotKey =
parallelRouteKey === 'children'
? childCacheKeyPart
: `@${encodeToFilesystemAndURLSafeString(parallelRouteKey)}/${childCacheKeyPart}`
return (parentSegmentKey + '/' + slotKey) as SegmentCacheKey
}
export function createHeadCacheKey(
renderedPathname: NormalizedPathname
): SegmentCacheKey {
// The "head" segment doesn't exist in the normal hierarchy of the page,
// but it needs to vary on all the route params. So we append the entire
// rendered pathname.
// TODO: Eventually, cache entries will only vary on params if they were
// actually accessed during server rendering. We'll do that by creating a
// separate key part for each of the route params (see: cache-map.ts). The
// same technique will also be used for the head segment.
return ('/_head/' +
encodeToFilesystemAndURLSafeString(renderedPathname)) as SegmentCacheKey
}
export function isHeadCacheKey(cacheKey: SegmentCacheKey): boolean {
return cacheKey.startsWith('/_head/')
}
// Define a regex pattern to match the most common characters found in a route // Define a regex pattern to match the most common characters found in a route
// param. It excludes anything that might not be cross-platform filesystem // param. It excludes anything that might not be cross-platform filesystem
// compatible, like |. It does not need to be precise because the fallback is to // compatible, like |. It does not need to be precise because the fallback is to
......
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