Unverified Commit 9fd1766f authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Search param fallback handling (#75990)

This implements search param handling in the Segment Cache.

When a cache entry is fetched via PPR, search params are treated as
dynamic data. We can share the same cache entry for every set of search
params for a given page segment.

By contrast, when a cache entry is fetched using a dynamic request (e.g.
using `<Link prefetch={true}`), the result may vary by the search param
values. So we must include the search params in the cache key.

During a navigation, we will first check for the more specific cache
entry (i.e. one that includes search params), and then fallback to the
PPR version if no such entry exists.
parent 6a30eb4e
......@@ -3,13 +3,17 @@ type Opaque<K, T> = T & { __brand: K }
// Only functions in this module should be allowed to create CacheKeys.
export type NormalizedHref = Opaque<'NormalizedHref', string>
export type NormalizedSearch = Opaque<'NormalizedSearch', string>
export type NormalizedNextUrl = Opaque<'NormalizedNextUrl', string>
export type RouteCacheKey = Opaque<
'RouteCacheKey',
{
href: NormalizedHref
search: NormalizedSearch
nextUrl: NormalizedNextUrl | null
// TODO: Eventually the dynamic params will be added here, too.
}
>
......@@ -18,20 +22,10 @@ export function createCacheKey(
nextUrl: string | null
): RouteCacheKey {
const originalUrl = new URL(originalHref)
// TODO: As of now, we never include search params in the cache key because
// per-segment prefetch requests are always static, and cannot contain search
// params. But to support <Link prefetch={true}>, we will sometimes populate
// the cache with dynamic data, so this will have to change.
originalUrl.search = ''
const normalizedHref = originalUrl.href as NormalizedHref
const normalizedNextUrl = nextUrl as NormalizedNextUrl | null
const cacheKey = {
href: normalizedHref,
nextUrl: normalizedNextUrl,
href: originalHref as NormalizedHref,
search: originalUrl.search as NormalizedSearch,
nextUrl: nextUrl as NormalizedNextUrl | null,
} as RouteCacheKey
return cacheKey
}
......@@ -23,7 +23,11 @@ import {
waitForSegmentCacheEntry,
type RouteTree,
} from './cache'
import { createCacheKey } from './cache-key'
import { createCacheKey, type RouteCacheKey } from './cache-key'
import {
addSearchParamsIfPageSegment,
PAGE_SEGMENT_KEY,
} from '../../../shared/lib/segment'
import { NavigationResultTag } from '../segment-cache'
type MPANavigationResult = {
......@@ -112,7 +116,7 @@ export function navigate(
const route = readRouteCacheEntry(now, cacheKey)
if (route !== null && route.status === EntryStatus.Fulfilled) {
// We have a matching prefetch.
const snapshot = readRenderSnapshotFromCache(now, route.tree)
const snapshot = readRenderSnapshotFromCache(now, cacheKey, route.tree)
const prefetchFlightRouterState = snapshot.flightRouterState
const prefetchSeedData = snapshot.seedData
const prefetchHead = route.head
......@@ -244,6 +248,7 @@ function navigationTaskToResult(
function readRenderSnapshotFromCache(
now: number,
routeCacheKey: RouteCacheKey,
tree: RouteTree
): { flightRouterState: FlightRouterState; seedData: CacheNodeSeedData } {
let childRouterStates: { [parallelRouteKey: string]: FlightRouterState } = {}
......@@ -254,7 +259,11 @@ function readRenderSnapshotFromCache(
if (slots !== null) {
for (const parallelRouteKey in slots) {
const childTree = slots[parallelRouteKey]
const childResult = readRenderSnapshotFromCache(now, childTree)
const childResult = readRenderSnapshotFromCache(
now,
routeCacheKey,
childTree
)
childRouterStates[parallelRouteKey] = childResult.flightRouterState
childSeedDatas[parallelRouteKey] = childResult.seedData
}
......@@ -264,7 +273,7 @@ function readRenderSnapshotFromCache(
let loading: LoadingModuleData | Promise<LoadingModuleData> = null
let isPartial: boolean = true
const segmentEntry = readSegmentCacheEntry(now, tree.key)
const segmentEntry = readSegmentCacheEntry(now, routeCacheKey, tree.key)
if (segmentEntry !== null) {
switch (segmentEntry.status) {
case EntryStatus.Fulfilled: {
......@@ -299,15 +308,33 @@ function readRenderSnapshotFromCache(
}
}
const segment =
tree.segment === PAGE_SEGMENT_KEY && routeCacheKey.search
? // The navigation implementation expects the search params to be
// included in the segment. However, the Segment Cache tracks search
// params separately from the rest of the segment key. So we need to
// add them back here.
//
// See corresponding comment in convertFlightRouterStateToTree.
//
// TODO: What we should do instead is update the navigation diffing
// logic to compare search params explicitly. This is a temporary
// solution until more of the Segment Cache implementation has settled.
addSearchParamsIfPageSegment(
tree.segment,
Object.fromEntries(new URLSearchParams(routeCacheKey.search))
)
: tree.segment
return {
flightRouterState: [
tree.segment,
segment,
childRouterStates,
null,
null,
tree.isRootLayout,
],
seedData: [tree.segment, rsc, childSeedDatas, loading, isPartial],
seedData: [segment, rsc, childSeedDatas, loading, isPartial],
}
}
......
......@@ -23,6 +23,7 @@ import {
upgradeToPendingSegment,
waitForSegmentCacheEntry,
resetRevalidatingSegmentEntry,
getSegmentKeypathForTask,
} from './cache'
import type { RouteCacheKey } from './cache-key'
import { PrefetchPriority } from '../segment-cache'
......@@ -480,6 +481,7 @@ function pingRootRouteTree(
const spawnedEntries = new Map<string, PendingSegmentCacheEntry>()
const dynamicRequestTree = diffRouteTreeAgainstCurrent(
now,
task,
route,
task.treeAtTimeOfPrefetch,
tree,
......@@ -520,7 +522,7 @@ function pingPPRRouteTree(
route: FulfilledRouteCacheEntry,
tree: RouteTree
): PrefetchTaskExitStatus.InProgress | PrefetchTaskExitStatus.Done {
const segment = readOrCreateSegmentCacheEntry(now, route, tree.key)
const segment = readOrCreateSegmentCacheEntry(now, task, route, tree.key)
pingPerSegment(now, task, route, segment, task.key, tree.key)
if (tree.slots !== null) {
if (!hasNetworkBandwidth()) {
......@@ -543,6 +545,7 @@ function pingPPRRouteTree(
function diffRouteTreeAgainstCurrent(
now: number,
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
oldTree: FlightRouterState,
newTree: RouteTree,
......@@ -576,6 +579,7 @@ function diffRouteTreeAgainstCurrent(
// This segment is already part of the current route. Keep traversing.
const requestTreeChild = diffRouteTreeAgainstCurrent(
now,
task,
route,
oldTreeChild,
newTreeChild,
......@@ -606,6 +610,7 @@ function diffRouteTreeAgainstCurrent(
const requestTreeChild =
pingPPRDisabledRouteTreeUpToLoadingBoundary(
now,
task,
route,
newTreeChild,
null,
......@@ -634,6 +639,7 @@ function diffRouteTreeAgainstCurrent(
// populate the segment cache.
const requestTreeChild = pingRouteTreeAndIncludeDynamicData(
now,
task,
route,
newTreeChild,
false,
......@@ -660,6 +666,7 @@ function diffRouteTreeAgainstCurrent(
function pingPPRDisabledRouteTreeUpToLoadingBoundary(
now: number,
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
tree: RouteTree,
refetchMarkerContext: 'refetch' | 'inside-shared-layout' | null,
......@@ -679,7 +686,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
let refetchMarker: 'refetch' | 'inside-shared-layout' | null =
refetchMarkerContext === null ? 'inside-shared-layout' : null
const segment = readOrCreateSegmentCacheEntry(now, route, tree.key)
const segment = readOrCreateSegmentCacheEntry(now, task, route, tree.key)
switch (segment.status) {
case EntryStatus.Empty: {
// This segment is not cached. Add a refetch marker so the server knows
......@@ -747,6 +754,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
requestTreeChildren[parallelRouteKey] =
pingPPRDisabledRouteTreeUpToLoadingBoundary(
now,
task,
route,
childTree,
refetchMarkerContext,
......@@ -766,6 +774,7 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
function pingRouteTreeAndIncludeDynamicData(
now: number,
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
tree: RouteTree,
isInsideRefetchingParent: boolean,
......@@ -779,7 +788,7 @@ function pingRouteTreeAndIncludeDynamicData(
// explicit "refetch" marker so the server knows where to start rendering.
// Once the server starts rendering along a path, it keeps rendering the
// entire subtree.
const segment = readOrCreateSegmentCacheEntry(now, route, tree.key)
const segment = readOrCreateSegmentCacheEntry(now, task, route, tree.key)
let spawnedSegment: PendingSegmentCacheEntry | null = null
......@@ -794,7 +803,13 @@ function pingRouteTreeAndIncludeDynamicData(
if (segment.isPartial) {
// The cached segment contians dynamic holes. Since this is a Full
// prefetch, we need to include it in the request.
spawnedSegment = pingFullSegmentRevalidation(now, segment, tree.key)
spawnedSegment = pingFullSegmentRevalidation(
now,
task,
route,
segment,
tree.key
)
}
break
}
......@@ -803,7 +818,13 @@ function pingRouteTreeAndIncludeDynamicData(
// There's either another prefetch currently in progress, or the previous
// attempt failed. If it wasn't a Full prefetch, fetch it again.
if (segment.fetchStrategy !== FetchStrategy.Full) {
spawnedSegment = pingFullSegmentRevalidation(now, segment, tree.key)
spawnedSegment = pingFullSegmentRevalidation(
now,
task,
route,
segment,
tree.key
)
}
break
}
......@@ -817,6 +838,7 @@ function pingRouteTreeAndIncludeDynamicData(
requestTreeChildren[parallelRouteKey] =
pingRouteTreeAndIncludeDynamicData(
now,
task,
route,
childTree,
isInsideRefetchingParent || spawnedSegment !== null,
......@@ -883,6 +905,7 @@ function pingPerSegment(
// `hasLoading` in the route tree prefetch response.
pingPPRSegmentRevalidation(
now,
task,
segment,
route,
routeKey,
......@@ -914,7 +937,14 @@ function pingPerSegment(
// Because a rejected segment will definitely prevent the segment (and
// all of its children) from rendering, we perform this revalidation
// immediately instead of deferring it to a background task.
pingPPRSegmentRevalidation(now, segment, route, routeKey, segmentKey)
pingPPRSegmentRevalidation(
now,
task,
segment,
route,
routeKey,
segmentKey
)
break
default:
segment.fetchStrategy satisfies never
......@@ -935,6 +965,7 @@ function pingPerSegment(
function pingPPRSegmentRevalidation(
now: number,
task: PrefetchTask,
currentSegment: SegmentCacheEntry,
route: FulfilledRouteCacheEntry,
routeKey: RouteCacheKey,
......@@ -949,6 +980,8 @@ function pingPPRSegmentRevalidation(
// Spawn a prefetch request and upsert the segment into the cache
// upon completion.
upsertSegmentOnCompletion(
task,
route,
segmentKey,
spawnPrefetchSubtask(
fetchSegmentOnCacheMiss(
......@@ -976,6 +1009,8 @@ function pingPPRSegmentRevalidation(
function pingFullSegmentRevalidation(
now: number,
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
currentSegment: SegmentCacheEntry,
segmentKey: string
): PendingSegmentCacheEntry | null {
......@@ -994,6 +1029,8 @@ function pingFullSegmentRevalidation(
FetchStrategy.Full
)
upsertSegmentOnCompletion(
task,
route,
segmentKey,
waitForSegmentCacheEntry(pendingSegment)
)
......@@ -1012,6 +1049,8 @@ function pingFullSegmentRevalidation(
FetchStrategy.Full
)
upsertSegmentOnCompletion(
task,
route,
segmentKey,
waitForSegmentCacheEntry(pendingSegment)
)
......@@ -1037,6 +1076,8 @@ function pingFullSegmentRevalidation(
const noop = () => {}
function upsertSegmentOnCompletion(
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
key: string,
promise: Promise<FulfilledSegmentCacheEntry | null>
) {
......@@ -1044,7 +1085,8 @@ function upsertSegmentOnCompletion(
promise.then((fulfilled) => {
if (fulfilled !== null) {
// Received new data. Attempt to replace the existing entry in the cache.
upsertSegmentEntry(Date.now(), key, fulfilled)
const keypath = getSegmentKeypathForTask(task, route, key)
upsertSegmentEntry(Date.now(), keypath, fulfilled)
}
}, noop)
}
......
import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment'
import type { Segment as FlightRouterStateSegment } from './types'
// TypeScript trick to simulate opaque types, like in Flow.
......@@ -9,6 +10,18 @@ export function encodeSegment(
segment: FlightRouterStateSegment
): EncodedSegment {
if (typeof segment === 'string') {
if (segment.startsWith(PAGE_SEGMENT_KEY)) {
// The Flight Router State type sometimes includes the search params in
// the page segment. However, the Segment Cache tracks this as a separate
// key. So, we strip the search params here, and then add them back when
// the cache entry is turned back into a FlightRouterState. This is an
// unfortunate consequence of the FlightRouteState being used both as a
// transport type and as a cache key; we'll address this once more of the
// Segment Cache implementation has settled.
// TODO: We should hoist the search params out of the FlightRouterState
// type entirely, This is our plan for dynamic route params, too.
return PAGE_SEGMENT_KEY as EncodedSegment
}
const safeName =
// TODO: FlightRouterState encodes Not Found routes as "/_not-found".
// But params typically don't include the leading slash. We should use
......
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from '../../components/link-accordion'
export default function SearchParamsPage() {
return (
<>
<p>
Demonstrates that we can prefetch a page that reads from search params
</p>
<ul>
<li>
<LinkAccordion href="/search-params/target-page?searchParam=a_PPR">
searchParam=a_PPR
</LinkAccordion>
</li>
<li>
<LinkAccordion
prefetch={true}
href="/search-params/target-page?searchParam=b_full"
>
searchParam=b_full, prefetch=true
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/search-params/target-page?searchParam=c_PPR">
searchParam=c_PPR
</LinkAccordion>
</li>
<li>
<LinkAccordion
prefetch={true}
href="/search-params/target-page?searchParam=d_full"
>
searchParam=d_full, prefetch=true
</LinkAccordion>
</li>
</ul>
</>
)
}
export default function SearchParamsLayout({ children }) {
return (
<div>
<p>Search params shared layout</p>
{children}
</div>
)
}
import { Suspense } from 'react'
async function Content({ searchParams }) {
const { searchParam } = await searchParams
return `Search param: ${searchParam}`
}
export default async function Target({ searchParams }) {
return (
<Suspense fallback="Loading...">
<div id="target-page-with-search-param">
<Content searchParams={searchParams} />
</div>
</Suspense>
)
}
'use client'
import Link from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: React.ReactNode
prefetch?: boolean
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link prefetch={prefetch} href={href}>
{children}
</Link>
) : (
<>{children} (link is hidden)</>
)}
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: true,
dynamicIO: true,
clientSegmentCache: true,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright'
import { createRouterAct } from '../router-act'
describe('segment cache (search params)', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (isNextDev || skipped) {
test('ppr is disabled', () => {})
return
}
it('when fetching with PPR, does not include search params in the cache key', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/search-params', {
beforePageLoad(page: Playwright.Page) {
act = createRouterAct(page)
},
})
// Prefetch a page with search param `a_PPR`.
const revealA = await browser.elementByCss(
'input[data-link-accordion="/search-params/target-page?searchParam=a_PPR"]'
)
await act(
async () => {
await revealA.click()
},
// The response will include a shell of the page, but nothing that is
// based on the search param.
{
includes:
// This is the id assigned to a container div
'target-page-with-search-param',
}
)
// Prefetch the same page but with the search param changed to `c_PPR`.
const revealC = await browser.elementByCss(
'input[data-link-accordion="/search-params/target-page?searchParam=c_PPR"]'
)
await act(
async () => {
await revealC.click()
},
// This should not issue a new request for the page segment, because
// search params are not included in the the PPR shell. So we can reuse
// the shell we fetched for `searchParam=a`.
{ includes: 'target-page-with-search-param', block: 'reject' }
)
// Navigate to one of the links.
const linkC = await browser.elementByCss(
'a[href="/search-params/target-page?searchParam=c_PPR"]'
)
await act(
async () => {
await linkC.click()
},
// The search param streams in on navigation
{
includes: 'Search param: c_PPR',
}
)
const result = await browser.elementById('target-page-with-search-param')
expect(await result.innerText()).toBe('Search param: c_PPR')
})
it('when fetching without PPR (e.g. prefetch={true}), includes the search params in the cache key', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/search-params', {
beforePageLoad(page: Playwright.Page) {
act = createRouterAct(page)
},
})
// Prefetch a page with search param `b_full`. This link has prefetch={true}
// so it will fetch the entire page, including the search param.
const revealB = await browser.elementByCss(
'input[data-link-accordion="/search-params/target-page?searchParam=b_full"]'
)
await act(
async () => {
await revealB.click()
},
// The response will include the entire page, including the search param.
{
includes: 'Search param: b_full',
}
)
// Prefetch a link with a different search param, and without
// prefetch={true}. This must fetch a new shell, because it can't use the
// entry we fetched for `searchParam=b_full` (because that one wasn't a
// shell — it included the search param).
const revealA = await browser.elementByCss(
'input[data-link-accordion="/search-params/target-page?searchParam=a_PPR"]'
)
await act(
async () => {
await revealA.click()
},
// The response will include a shell of the page, but nothing that is
// based on the search param.
{ includes: 'target-page-with-search-param' }
)
// Prefetch a different link using prefetch={true}. Again, this must issue
// a new request, because it's a full page prefetch and we haven't fetched
// this particular search param before.
// TODO: As an future optimization, if a navigation to this link occurs
// before the prefetch completes, we could render the PPR shell in
// the meantime, since it contains no search params. This would effectively
// be a "per-segment fallback".
const revealD = await browser.elementByCss(
'input[data-link-accordion="/search-params/target-page?searchParam=d_full"]'
)
await act(
async () => {
await revealD.click()
},
// The response will include the entire page, including the search param.
{ includes: 'Search param: d_full' }
)
// Navigate to one of the fully prefetched links.
const linkD = await browser.elementByCss(
'a[href="/search-params/target-page?searchParam=d_full"]'
)
await act(
async () => {
await linkD.click()
const result = await browser.elementById(
'target-page-with-search-param'
)
expect(await result.innerText()).toBe('Search param: d_full')
},
// No requests should be issued, because the page was fully prefetched.
'no-requests'
)
})
})
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