Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-use loading from prefetch cache entries across searchParams #68340

Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe('createInitialRouterState', () => {
lastUsedTime: expect.any(Number),
treeAtTimeOfPrefetch: initialTree,
status: PrefetchCacheEntryStatus.fresh,
pathname: '/linking',
},
],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createHrefFromUrl } from './create-href-from-url'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { extractPathFromFlightRouterState } from './compute-changed-path'
import { createPrefetchCacheEntryForInitialLoad } from './prefetch-cache-utils'
import { PrefetchKind, type PrefetchCacheEntry } from './router-reducer-types'
import type { PrefetchCacheEntry } from './router-reducer-types'
import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-parallel-segments'

export interface InitialRouterStateParameters {
Expand Down Expand Up @@ -101,7 +101,6 @@ export function createInitialRouterState({

createPrefetchCacheEntryForInitialLoad({
url,
kind: PrefetchKind.AUTO,
data: {
f: initialFlightData,
c: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type {
FlightDataPath,
CacheNodeSeedData,
} from '../../../server/app-render/types'
import type { FlightDataPath } from '../../../server/app-render/types'
import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { createRouterCacheKey } from './create-router-cache-key'
import type { PrefetchCacheEntry } from './router-reducer-types'

/**
* Fill cache with rsc based on flightDataPath
* Common logic for filling cache with new sub tree data.
*/
export function fillCacheWithNewSubTreeData(
function fillCacheHelper(
newCache: CacheNode,
existingCache: CacheNode,
flightDataPath: FlightDataPath,
prefetchEntry?: PrefetchCacheEntry
prefetchEntry: PrefetchCacheEntry | undefined,
fillLazyItems: boolean
): void {
const isLastEntry = flightDataPath.length <= 5
const [parallelRouteKey, segment] = flightDataPath
Expand All @@ -39,45 +37,47 @@ export function fillCacheWithNewSubTreeData(

const existingChildCacheNode = existingChildSegmentMap.get(cacheKey)
let childCacheNode = childSegmentMap.get(cacheKey)
const cacheNodeSeedData = flightDataPath[3]

if (isLastEntry) {
if (
!childCacheNode ||
!childCacheNode.lazyData ||
childCacheNode === existingChildCacheNode
cacheNodeSeedData &&
(!childCacheNode ||
!childCacheNode.lazyData ||
childCacheNode === existingChildCacheNode)
) {
const seedData: CacheNodeSeedData = flightDataPath[3]
const rsc = seedData[2]
const loading = seedData[3]
const rsc = cacheNodeSeedData[2]
const loading = cacheNodeSeedData[3]
childCacheNode = {
lazyData: null,
rsc,
prefetchRsc: null,
head: null,
prefetchHead: null,
loading,
// Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes)
: new Map(),
parallelRoutes:
fillLazyItems && existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes)
: new Map(),
}

if (existingChildCacheNode) {
if (existingChildCacheNode && fillLazyItems) {
invalidateCacheByRouterState(
childCacheNode,
existingChildCacheNode,
flightDataPath[2]
)
}

fillLazyItemsTillLeafWithHead(
childCacheNode,
existingChildCacheNode,
flightDataPath[2],
seedData,
flightDataPath[4],
prefetchEntry
)
if (fillLazyItems) {
fillLazyItemsTillLeafWithHead(
childCacheNode,
existingChildCacheNode,
flightDataPath[2],
cacheNodeSeedData,
flightDataPath[4],
prefetchEntry
)
}

childSegmentMap.set(cacheKey, childCacheNode)
}
Expand All @@ -103,10 +103,32 @@ export function fillCacheWithNewSubTreeData(
childSegmentMap.set(cacheKey, childCacheNode)
}

fillCacheWithNewSubTreeData(
fillCacheHelper(
childCacheNode,
existingChildCacheNode,
flightDataPath.slice(2),
prefetchEntry
prefetchEntry,
fillLazyItems
)
}

/**
* Fill cache with rsc based on flightDataPath
*/
export function fillCacheWithNewSubTreeData(
newCache: CacheNode,
existingCache: CacheNode,
flightDataPath: FlightDataPath,
prefetchEntry?: PrefetchCacheEntry
): void {
fillCacheHelper(newCache, existingCache, flightDataPath, prefetchEntry, true)
}

export function fillCacheWithNewSubTreeDataButOnlyLoading(
newCache: CacheNode,
existingCache: CacheNode,
flightDataPath: FlightDataPath,
prefetchEntry?: PrefetchCacheEntry
): void {
fillCacheHelper(newCache, existingCache, flightDataPath, prefetchEntry, false)
}
Loading
Loading