Unverified Commit 608ece13 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

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

### What & Why
A route segment's `loading.tsx` file won't be re-used when navigating
between the same route but with different search parameters. This means
that the loading state won't be shown until it's streamed in as part of
the dynamic request. This is because the prefetch cache is keyed by both
the pathname and search string. A workaround would be to optimistically
prefetch the route + search params of the page you were going to, but
often this is not known ahead of time.

Additionally, when you have a bunch of links to the same route segment
but with search params, you'll have an equal number of prefetch requests
to that URL, which is wasteful since each one will return the same
partial loading data.

### How
Because `layout.tsx` and `loading.tsx` don't receive access to
`searchParams`, we're able to reliably re-use the loading state between
multiple routes within the same route segment. Only "full" prefetches
need to be keyed by the search string, since those could contain
responses that that are dependent on the provided params.

This refactors the prefetch cache getter logic to look for a suitable
"alias" when it would have otherwise been a cache miss. It does this by
comparing pathnames of the cache entries: if there's a pathname match,
we'll mark it as an alias. The navigation reducer will use this
knowledge to only apply the `loading` node and not the actual segment
data, so that the router can still make the lazy fetch to get the full
dynamic data.

Closes NDX-132
parent f1ffc918
...@@ -115,6 +115,7 @@ describe('createInitialRouterState', () => { ...@@ -115,6 +115,7 @@ describe('createInitialRouterState', () => {
lastUsedTime: expect.any(Number), lastUsedTime: expect.any(Number),
treeAtTimeOfPrefetch: initialTree, treeAtTimeOfPrefetch: initialTree,
status: PrefetchCacheEntryStatus.fresh, status: PrefetchCacheEntryStatus.fresh,
pathname: '/linking',
}, },
], ],
]), ]),
......
...@@ -5,7 +5,7 @@ import { createHrefFromUrl } from './create-href-from-url' ...@@ -5,7 +5,7 @@ import { createHrefFromUrl } from './create-href-from-url'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head' import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { extractPathFromFlightRouterState } from './compute-changed-path' import { extractPathFromFlightRouterState } from './compute-changed-path'
import { createPrefetchCacheEntryForInitialLoad } from './prefetch-cache-utils' 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' import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-parallel-segments'
export interface InitialRouterStateParameters { export interface InitialRouterStateParameters {
...@@ -101,7 +101,6 @@ export function createInitialRouterState({ ...@@ -101,7 +101,6 @@ export function createInitialRouterState({
createPrefetchCacheEntryForInitialLoad({ createPrefetchCacheEntryForInitialLoad({
url, url,
kind: PrefetchKind.AUTO,
data: { data: {
f: initialFlightData, f: initialFlightData,
c: undefined, c: undefined,
......
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime' import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type { import type { FlightDataPath } from '../../../server/app-render/types'
FlightDataPath,
CacheNodeSeedData,
} from '../../../server/app-render/types'
import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state' import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head' import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { createRouterCacheKey } from './create-router-cache-key' import { createRouterCacheKey } from './create-router-cache-key'
import type { PrefetchCacheEntry } from './router-reducer-types' 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, newCache: CacheNode,
existingCache: CacheNode, existingCache: CacheNode,
flightDataPath: FlightDataPath, flightDataPath: FlightDataPath,
prefetchEntry?: PrefetchCacheEntry prefetchEntry: PrefetchCacheEntry | undefined,
fillLazyItems: boolean
): void { ): void {
const isLastEntry = flightDataPath.length <= 5 const isLastEntry = flightDataPath.length <= 5
const [parallelRouteKey, segment] = flightDataPath const [parallelRouteKey, segment] = flightDataPath
...@@ -39,16 +37,17 @@ export function fillCacheWithNewSubTreeData( ...@@ -39,16 +37,17 @@ export function fillCacheWithNewSubTreeData(
const existingChildCacheNode = existingChildSegmentMap.get(cacheKey) const existingChildCacheNode = existingChildSegmentMap.get(cacheKey)
let childCacheNode = childSegmentMap.get(cacheKey) let childCacheNode = childSegmentMap.get(cacheKey)
const cacheNodeSeedData = flightDataPath[3]
if (isLastEntry) { if (isLastEntry) {
if ( if (
!childCacheNode || cacheNodeSeedData &&
(!childCacheNode ||
!childCacheNode.lazyData || !childCacheNode.lazyData ||
childCacheNode === existingChildCacheNode childCacheNode === existingChildCacheNode)
) { ) {
const seedData: CacheNodeSeedData = flightDataPath[3] const rsc = cacheNodeSeedData[2]
const rsc = seedData[2] const loading = cacheNodeSeedData[3]
const loading = seedData[3]
childCacheNode = { childCacheNode = {
lazyData: null, lazyData: null,
rsc, rsc,
...@@ -56,28 +55,29 @@ export function fillCacheWithNewSubTreeData( ...@@ -56,28 +55,29 @@ export function fillCacheWithNewSubTreeData(
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading, loading,
// Ensure segments other than the one we got data for are preserved. parallelRoutes:
parallelRoutes: existingChildCacheNode fillLazyItems && existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes) ? new Map(existingChildCacheNode.parallelRoutes)
: new Map(), : new Map(),
} }
if (existingChildCacheNode) { if (existingChildCacheNode && fillLazyItems) {
invalidateCacheByRouterState( invalidateCacheByRouterState(
childCacheNode, childCacheNode,
existingChildCacheNode, existingChildCacheNode,
flightDataPath[2] flightDataPath[2]
) )
} }
if (fillLazyItems) {
fillLazyItemsTillLeafWithHead( fillLazyItemsTillLeafWithHead(
childCacheNode, childCacheNode,
existingChildCacheNode, existingChildCacheNode,
flightDataPath[2], flightDataPath[2],
seedData, cacheNodeSeedData,
flightDataPath[4], flightDataPath[4],
prefetchEntry prefetchEntry
) )
}
childSegmentMap.set(cacheKey, childCacheNode) childSegmentMap.set(cacheKey, childCacheNode)
} }
...@@ -103,10 +103,32 @@ export function fillCacheWithNewSubTreeData( ...@@ -103,10 +103,32 @@ export function fillCacheWithNewSubTreeData(
childSegmentMap.set(cacheKey, childCacheNode) childSegmentMap.set(cacheKey, childCacheNode)
} }
fillCacheWithNewSubTreeData( fillCacheHelper(
childCacheNode, childCacheNode,
existingChildCacheNode, existingChildCacheNode,
flightDataPath.slice(2), 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)
}
...@@ -30,6 +30,7 @@ import { ...@@ -30,6 +30,7 @@ import {
prunePrefetchCache, prunePrefetchCache,
} from '../prefetch-cache-utils' } from '../prefetch-cache-utils'
import { clearCacheNodeDataForSegmentPath } from '../clear-cache-node-data-for-segment-path' import { clearCacheNodeDataForSegmentPath } from '../clear-cache-node-data-for-segment-path'
import { fillCacheWithNewSubTreeDataButOnlyLoading } from '../fill-cache-with-new-subtree-data'
export function handleExternalUrl( export function handleExternalUrl(
state: ReadonlyReducerState, state: ReadonlyReducerState,
...@@ -209,7 +210,35 @@ function navigateReducer_noPPR( ...@@ -209,7 +210,35 @@ function navigateReducer_noPPR(
const cache: CacheNode = createEmptyCacheNode() const cache: CacheNode = createEmptyCacheNode()
let applied = false let applied = false
if ( // The prefetch cache entry was aliased -- this signals that we only fill in the cache with the
// loading state and not the actual parallel route seed data.
if (prefetchValues.aliased) {
// Root render
if (flightDataPath.length === 3) {
// Fill in the cache with the new loading / rsc data
const cacheNodeSeedData = flightDataPath[1]
const rsc = cacheNodeSeedData[2]
const loading = cacheNodeSeedData[3]
cache.loading = loading
cache.rsc = rsc
} else {
// Copy rsc for the root node of the cache.
cache.rsc = currentCache.rsc
cache.prefetchRsc = currentCache.prefetchRsc
cache.loading = currentCache.loading
cache.parallelRoutes = new Map(currentCache.parallelRoutes)
// recursively fill in `rsc` and `loading` but skip everything else
fillCacheWithNewSubTreeDataButOnlyLoading(
cache,
currentCache,
flightDataPath,
prefetchValues
)
}
applied = true
} else if (
prefetchValues.status === PrefetchCacheEntryStatus.stale && prefetchValues.status === PrefetchCacheEntryStatus.stale &&
!mutable.onlyHashChange && !mutable.onlyHashChange &&
!isFirstRead !isFirstRead
...@@ -406,7 +435,8 @@ function navigateReducer_PPR( ...@@ -406,7 +435,8 @@ function navigateReducer_PPR(
// TODO: We should get rid of the else branch and do all navigations // TODO: We should get rid of the else branch and do all navigations
// via updateCacheNodeOnNavigation. The current structure is just // via updateCacheNodeOnNavigation. The current structure is just
// an incremental step. // an incremental step.
flightDataPath.length === 3 flightDataPath.length === 3 &&
!prefetchValues.aliased
) { ) {
const prefetchedTree: FlightRouterState = flightDataPath[0] const prefetchedTree: FlightRouterState = flightDataPath[0]
const seedData = flightDataPath[1] const seedData = flightDataPath[1]
...@@ -479,7 +509,35 @@ function navigateReducer_PPR( ...@@ -479,7 +509,35 @@ function navigateReducer_PPR(
const cache: CacheNode = createEmptyCacheNode() const cache: CacheNode = createEmptyCacheNode()
let applied = false let applied = false
if ( // The prefetch cache entry was aliased -- this signals that we only fill in the cache with the
// loading state and not the actual parallel route seed data.
if (prefetchValues.aliased) {
// Root render
if (flightDataPath.length === 3) {
// Fill in the cache with the new loading / rsc data
const cacheNodeSeedData = flightDataPath[1]
const rsc = cacheNodeSeedData[2]
const loading = cacheNodeSeedData[3]
cache.loading = loading
cache.rsc = rsc
} else {
// Copy rsc for the root node of the cache.
cache.rsc = currentCache.rsc
cache.prefetchRsc = currentCache.prefetchRsc
cache.loading = currentCache.loading
cache.parallelRoutes = new Map(currentCache.parallelRoutes)
// recursively fill in `rsc` and `loading` but skip everything else
fillCacheWithNewSubTreeDataButOnlyLoading(
cache,
currentCache,
flightDataPath,
prefetchValues
)
}
applied = true
} else if (
prefetchValues.status === PrefetchCacheEntryStatus.stale && prefetchValues.status === PrefetchCacheEntryStatus.stale &&
!mutable.onlyHashChange && !mutable.onlyHashChange &&
!isFirstRead !isFirstRead
......
...@@ -207,6 +207,7 @@ export type PrefetchCacheEntry = { ...@@ -207,6 +207,7 @@ export type PrefetchCacheEntry = {
lastUsedTime: number | null lastUsedTime: number | null
key: string key: string
status: PrefetchCacheEntryStatus status: PrefetchCacheEntryStatus
pathname: string
} }
export enum PrefetchCacheEntryStatus { export enum PrefetchCacheEntryStatus {
......
...@@ -178,8 +178,6 @@ describe('app dir - navigation', () => { ...@@ -178,8 +178,6 @@ describe('app dir - navigation', () => {
if (isNextStart || isNextDeploy) { if (isNextStart || isNextDeploy) {
await browser.waitForIdleNetwork() await browser.waitForIdleNetwork()
// there should be an RSC call for the prefetch
expect(hasRscRequest).toBe(true)
} }
// Wait for all network requests to finish, and then initialize the flag // Wait for all network requests to finish, and then initialize the flag
......
import { ReactNode, Suspense } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>
<Suspense>{children}</Suspense>
</body>
</html>
)
}
'use client'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
export default function Page() {
const router = useRouter()
return (
<>
<div>
<Link href="/search-params?id=1" prefetch>
/search-params?id=1 (prefetch: true)
</Link>
</div>
<button
onClick={() => {
router.push('/search-params')
}}
>
Navigate to /search-params
</button>
</>
)
}
'use client'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
export default function Page() {
const router = useRouter()
return (
<>
<div>
<Link href="/search-params" prefetch>
/search-params (prefetch: true)
</Link>
</div>
<button
onClick={() => {
router.push('/search-params?id=1')
}}
>
Navigate to /search-params?id=1
</button>
</>
)
}
'use client'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
export default function Page() {
const router = useRouter()
return (
<>
<div>
<Link href="/search-params?id=1" prefetch>
/search-params?id=1 (prefetch: true)
</Link>
</div>
<button
onClick={() => {
router.push('/search-params?id=2')
}}
>
Navigate to /search-params?id=2
</button>
</>
)
}
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/search">Go to search</Link>
<hr />
<ul>
<li>
<Link href="/search-params?id=1">/search-params?id=1</Link>
</li>
<li>
<Link href="/search-params?id=2">/search-params?id=2</Link>
</li>
<li>
<Link href="/search-params?id=3" prefetch>
/search-params?id=3 (prefetch: true)
</Link>
</li>
<li>
<Link href="/search-params" prefetch>
/search-params (prefetch: true)
</Link>
</li>
</ul>
</>
)
}
export default function Loading() {
return <h1 id="loading">Loading...</h1>
}
import Link from 'next/link'
export default async function Page({
searchParams,
}: {
searchParams: Record<string, string>
}) {
// sleep for 500ms
await new Promise((resolve) => setTimeout(resolve, 500))
return (
<>
<h1 id="params">{JSON.stringify(searchParams)}</h1>
<Link href="/">Back</Link>
</>
)
}
'use client'
import { Fragment } from 'react'
import { useSearchParams } from 'next/navigation'
export default function SearchLayout({
children,
}: {
children: React.ReactNode
}) {
let searchParams = useSearchParams()
return <Fragment key={searchParams.get('q')}>{children}</Fragment>
}
export default function Loading() {
return <p id="loading">Loading...</p>
}
import { Search } from './search'
export default async function Page({ searchParams }: { searchParams: any }) {
await new Promise((resolve) => setTimeout(resolve, 3000))
return (
<main id="page-content">
<Search />
<p id="search-value">Search Value: {searchParams.q ?? 'None'}</p>
</main>
)
}
'use client'
import { useRouter } from 'next/navigation'
export function Search() {
let router = useRouter()
function search(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault()
let input = event.currentTarget.q.value
let params = new URLSearchParams([['q', input]])
router.push(`/search?${params}`)
}
return (
<form onSubmit={search}>
<input placeholder="Search..." name="q" className="border" />
<button>Submit</button>
</form>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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