Unverified Commit d27f18f3 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Cache the head separately from the route tree (#84724)

Currently the head (i.e. the viewport and the metadata) is stored in the
same cache entry as the route tree. This is not ideal because the head
may or may not contain application data, and the response can differ
depending on the prefetch strategy. We need to be able to re-fetch the
head without also invalidating the route tree.

This refactors the Segment Cache to treat the head data as a special
kind of "segment". It doesn't appear in the route tree, so there's some
logic to account for that, but for the most part it reuses all the
existing implementation for fetching and caching normal segment data.
parent efbc187d
......@@ -122,8 +122,9 @@ export function navigate(
const snapshot = readRenderSnapshotFromCache(now, route, route.tree)
const prefetchFlightRouterState = snapshot.flightRouterState
const prefetchSeedData = snapshot.seedData
const prefetchHead = route.head
const isPrefetchHeadPartial = route.isHeadPartial
const headSnapshot = readHeadSnapshotFromCache(now, route)
const prefetchHead = headSnapshot.rsc
const isPrefetchHeadPartial = headSnapshot.isPartial
// TODO: The "canonicalUrl" stored in the cache doesn't include the hash,
// because hash entries do not vary by hash fragment. However, the one
// we set in the router state *does* include the hash, and it's used to
......@@ -171,8 +172,9 @@ export function navigate(
)
const prefetchFlightRouterState = snapshot.flightRouterState
const prefetchSeedData = snapshot.seedData
const prefetchHead = optimisticRoute.head
const isPrefetchHeadPartial = optimisticRoute.isHeadPartial
const headSnapshot = readHeadSnapshotFromCache(now, optimisticRoute)
const prefetchHead = headSnapshot.rsc
const isPrefetchHeadPartial = headSnapshot.isPartial
const newCanonicalUrl = optimisticRoute.canonicalUrl + url.hash
const newRenderedSearch = optimisticRoute.renderedSearch
return navigateUsingPrefetchedRouteTree(
......@@ -414,6 +416,43 @@ function readRenderSnapshotFromCache(
}
}
function readHeadSnapshotFromCache(
now: number,
route: FulfilledRouteCacheEntry
): { rsc: HeadData; isPartial: boolean } {
// Same as readRenderSnapshotFromCache, but for the head
let rsc: React.ReactNode | null = null
let isPartial: boolean = true
const canonicalSegmentKeypath = getCanonicalSegmentKeypath(
route,
route.metadata.cacheKey
)
const segmentEntry = readSegmentCacheEntry(now, canonicalSegmentKeypath)
if (segmentEntry !== null) {
switch (segmentEntry.status) {
case EntryStatus.Fulfilled: {
rsc = segmentEntry.rsc
isPartial = segmentEntry.isPartial
break
}
case EntryStatus.Pending: {
const promiseForFulfilledEntry = waitForSegmentCacheEntry(segmentEntry)
rsc = promiseForFulfilledEntry.then((entry) =>
entry !== null ? entry.rsc : null
)
isPartial = true
break
}
case EntryStatus.Empty:
case EntryStatus.Rejected:
break
default:
segmentEntry satisfies never
}
}
return { rsc, isPartial }
}
async function navigateDynamicallyWithNoPrefetch(
now: number,
url: URL,
......
......@@ -674,6 +674,7 @@ function pingRootRouteTree(
//
// Then, if there are any segments that need a runtime request,
// do another pass to perform a runtime prefetch.
pingStaticHead(now, task, route)
const exitStatus = pingSharedPartOfCacheComponentsTree(
now,
task,
......@@ -693,6 +694,13 @@ function pingRootRouteTree(
SegmentCacheKey,
PendingSegmentCacheEntry
>()
pingRuntimeHead(
now,
task,
route,
spawnedEntries,
FetchStrategy.PPRRuntime
)
const requestTree = pingRuntimePrefetches(
now,
task,
......@@ -732,6 +740,7 @@ function pingRootRouteTree(
SegmentCacheKey,
PendingSegmentCacheEntry
>()
pingRuntimeHead(now, task, route, spawnedEntries, fetchStrategy)
const dynamicRequestTree = diffRouteTreeAgainstCurrent(
now,
task,
......@@ -741,41 +750,8 @@ function pingRootRouteTree(
spawnedEntries,
fetchStrategy
)
let needsDynamicRequest = spawnedEntries.size > 0
if (
!needsDynamicRequest &&
route.isHeadPartial &&
route.TODO_metadataStatus === EntryStatus.Empty
) {
// All the segment data is already cached, however, we need to issue
// a request anyway so we can prefetch the head. Update the status
// field to prevent additional requests from being spawned while
// this one is in progress.
// TODO: This is a temporary, targeted solution to fix a regression
// we found. It exists to prevent the scheduler from sending a
// redundant request if there's already one in progress.
// Essentially, it will attempt once at most, then give up until the
// route entry expires or is evicted by other means. But because
// this doesn't have its own stale time separate from the route
// itself, there will be edge cases where the metadata fails to be
// fully prefetched. Consider caching metadata using a separate
// entry type so we can model this more cleanly. The circumstances
// that lead to this branch running in the first place are
// relatively rare, so it's not critical.
route.TODO_metadataStatus = EntryStatus.Fulfilled
needsDynamicRequest = true
// This instructs the server to only send the metadata.
dynamicRequestTree[3] = 'metadata-only'
// We can null out the children to reduce the request size, since
// they won't be needed.
dynamicRequestTree[1] = {}
}
if (needsDynamicRequest) {
// Perform a dynamic prefetch request and populate the cache with
// the result
spawnPrefetchSubtask(
fetchSegmentPrefetchesUsingDynamicRequest(
task,
......@@ -800,6 +776,56 @@ function pingRootRouteTree(
return PrefetchTaskExitStatus.Done
}
function pingStaticHead(
now: number,
task: PrefetchTask,
route: FulfilledRouteCacheEntry
): void {
// The Head data for a page (metadata, viewport) is not really a route
// segment, in the sense that it doesn't appear in the route tree. But we
// store it in the cache as if it were, using a special key.
pingStaticSegmentData(
now,
task,
route,
readOrCreateSegmentCacheEntry(
now,
FetchStrategy.PPR,
route,
route.metadata.cacheKey
),
task.key,
route.metadata
)
}
function pingRuntimeHead(
now: number,
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
spawnedEntries: Map<string, PendingSegmentCacheEntry>,
fetchStrategy:
| FetchStrategy.Full
| FetchStrategy.PPRRuntime
| FetchStrategy.LoadingBoundary
): void {
pingRouteTreeAndIncludeDynamicData(
now,
task,
route,
route.metadata,
false,
spawnedEntries,
// When prefetching the head, there's no difference between Full
// and LoadingBoundary
fetchStrategy === FetchStrategy.LoadingBoundary
? FetchStrategy.Full
: fetchStrategy
)
}
// TODO: Rename dynamic -> runtime throughout this module
function pingSharedPartOfCacheComponentsTree(
now: number,
task: PrefetchTask,
......
......@@ -19,7 +19,10 @@
export type { NavigationResult } from './segment-cache-impl/navigation'
export type { PrefetchTask } from './segment-cache-impl/scheduler'
export type { NormalizedSearch } from './segment-cache-impl/cache-key'
export type {
NormalizedPathname,
NormalizedSearch,
} from './segment-cache-impl/cache-key'
const notEnabled: any = () => {
throw new Error(
......
......@@ -10,7 +10,10 @@ import {
NEXT_REWRITTEN_QUERY_HEADER,
NEXT_RSC_UNION_QUERY,
} from './components/app-router-headers'
import type { NormalizedSearch } from './components/segment-cache'
import type {
NormalizedPathname,
NormalizedSearch,
} from './components/segment-cache'
import type { RSCResponse } from './components/router-reducer/fetch-server-response'
import type { ParsedUrlQuery } from 'querystring'
......@@ -42,14 +45,14 @@ export function getRenderedSearch(
export function getRenderedPathname(
response: RSCResponse<unknown> | Response
): string {
): NormalizedPathname {
// If the server performed a rewrite, the pathname used to render the
// page will be different from the pathname in the request URL. In this case,
// the response will include a header that gives the rewritten pathname.
const rewrittenPath = response.headers.get(NEXT_REWRITTEN_PATH_HEADER)
return (
rewrittenPath ?? urlToUrlWithoutFlightMarker(new URL(response.url)).pathname
)
return (rewrittenPath ??
urlToUrlWithoutFlightMarker(new URL(response.url))
.pathname) as NormalizedPathname
}
export function parseDynamicParamFromURLPart(
......
......@@ -24,6 +24,7 @@ import {
createSegmentRequestKeyPart,
appendSegmentRequestKeyPart,
ROOT_SEGMENT_REQUEST_KEY,
HEAD_REQUEST_KEY,
} from '../../shared/lib/segment-cache/segment-value-encoding'
import { getDigestForWellKnownError } from './create-error-handler'
......@@ -32,8 +33,6 @@ import { getDigestForWellKnownError } from './create-error-handler'
export type RootTreePrefetch = {
buildId: string
tree: TreePrefetch
head: HeadData
isHeadPartial: boolean
staleTime: number
}
......@@ -225,7 +224,21 @@ async function PrefetchTreeData({
segmentTasks
)
const isHeadPartial = await isPartialRSCData(head, clientModules)
// Also spawn a task to produce a prefetch response for the "head" segment.
// The head contains metadata, like the title; it's not really a route
// segment, but it contains RSC data, so it's treated like a segment by
// the client cache.
segmentTasks.push(
waitAtLeastOneReactRenderTask().then(() =>
renderSegmentPrefetch(
buildId,
head,
null,
HEAD_REQUEST_KEY,
clientModules
)
)
)
// Notify the abort controller that we're done processing the route tree.
// Anything async that happens after this point must be due to hanging
......@@ -236,8 +249,6 @@ async function PrefetchTreeData({
const treePrefetch: RootTreePrefetch = {
buildId,
tree,
head,
isHeadPartial,
staleTime,
}
return treePrefetch
......@@ -292,7 +303,13 @@ function collectSegmentDataImpl(
// Since we're already in the middle of a render, wait until after the
// current task to escape the current rendering context.
waitAtLeastOneReactRenderTask().then(() =>
renderSegmentPrefetch(buildId, seedData, requestKey, clientModules)
renderSegmentPrefetch(
buildId,
seedData[0],
seedData[2],
requestKey,
clientModules
)
)
)
} else {
......@@ -333,15 +350,14 @@ function collectSegmentDataImpl(
async function renderSegmentPrefetch(
buildId: string,
seedData: CacheNodeSeedData,
rsc: React.ReactNode,
loading: LoadingModuleData | Promise<LoadingModuleData>,
requestKey: SegmentRequestKey,
clientModules: ManifestNode
): Promise<[SegmentRequestKey, Buffer]> {
// Render the segment data to a stream.
// In the future, this is where we can include additional metadata, like the
// stale time and cache tags.
const rsc = seedData[0]
const loading = seedData[2]
const segmentPrefetch: SegmentPrefetch = {
buildId,
rsc,
......
import { PAGE_SEGMENT_KEY } from '../segment'
import type { Segment as FlightRouterStateSegment } from '../app-router-types'
import type { NormalizedPathname } from '../../../client/components/segment-cache'
// TypeScript trick to simulate opaque types, like in Flow.
type Opaque<K, T> = T & { __brand: K }
......@@ -12,6 +13,8 @@ export type SegmentCacheKey = Opaque<'SegmentCacheKey', string>
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 function createSegmentRequestKeyPart(
segment: FlightRouterStateSegment
): SegmentRequestKeyPart {
......@@ -92,6 +95,24 @@ export function appendSegmentCacheKeyPart(
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
// 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
......
......@@ -42,17 +42,12 @@ describe('segment cache (metadata)', () => {
)
await checkbox.click()
}, [
// TODO: Ideally, this would not prefetch the dynamic title again,
// because it was already prefetched by the previous link, and both
// links resolve to the same underlying route. This is because, unlike
// segment data, we cache routes solely by their input URL, not by the
// path of the underlying route. Similarly, we don't cache metadata
// separately from the route tree. We should probably do one or both.
// It should not prefetch the page title or content again, because it
// was already cached.
{
includes: 'Dynamic Title',
block: 'reject',
},
// It should not prefetch the page content again, because it was
// already cached.
{
includes: 'Target page',
block: 'reject',
......@@ -107,17 +102,12 @@ describe('segment cache (metadata)', () => {
)
await checkbox.click()
}, [
// TODO: Ideally, this would not prefetch the dynamic title again,
// because it was already prefetched by the previous link, and both
// links resolve to the same underlying route. This is because, unlike
// segment data, we cache routes solely by their input URL, not by the
// path of the underlying route. Similarly, we don't cache metadata
// separately from the route tree. We should probably do one or both.
// It should not prefetch the page title or content again, because it
// was already cached.
{
includes: 'Runtime-prefetchable title',
block: 'reject',
},
// It should not prefetch the page content again, because it was
// already cached.
{
includes: 'Target page',
block: 'reject',
......
......@@ -51,7 +51,11 @@ describe('segment cache prefetch scheduling', () => {
)
})
it('prioritizes prefetching the route trees before the segments', async () => {
// TODO: This test no longer works as-written because the metadata is now
// fetched separately from the route tree. Need to rewrite to assert on
// something that appears in the route tree and is relatively stable, like a
// static route name.
it.skip('prioritizes prefetching the route trees before the segments', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/cancellation', {
beforePageLoad(p: Playwright.Page) {
......
......@@ -126,7 +126,7 @@ describe('middleware-static-rewrite', () => {
const url = new URL('/my-team', 'http://localhost')
const rsc = computeCacheBustingSearchParam(
'1',
'/_tree',
'/_head',
undefined,
undefined
)
......@@ -138,7 +138,7 @@ describe('middleware-static-rewrite', () => {
Cookie: 'overview-param=grid',
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-Segment-Prefetch': '/_tree',
'Next-Router-Segment-Prefetch': '/_head',
},
})
......@@ -174,7 +174,7 @@ describe('middleware-static-rewrite', () => {
Cookie: 'overview-param=grid',
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-Segment-Prefetch': '/_tree',
'Next-Router-Segment-Prefetch': '/_head',
},
})
......
......@@ -33,6 +33,7 @@ export const expectedWhenTrailingSlashTrue = [
'404/index.html',
'__next.__PAGE__.txt',
'__next._full.txt',
'__next._head.txt',
'__next._index.txt',
'__next._tree.txt',
// Turbopack and plain next.js have different hash output for the file name
......@@ -47,6 +48,7 @@ export const expectedWhenTrailingSlashTrue = [
: []),
'_next/static/test-build-id/_ssgManifest.js',
'_not-found/__next._full.txt',
'_not-found/__next._head.txt',
'_not-found/__next._index.txt',
'_not-found/__next._not-found.__PAGE__.txt',
'_not-found/__next._not-found.txt',
......@@ -54,11 +56,13 @@ export const expectedWhenTrailingSlashTrue = [
'_not-found/index.html',
'_not-found/index.txt',
'another/__next._full.txt',
'another/__next._head.txt',
'another/__next._index.txt',
'another/__next._tree.txt',
'another/__next.another.__PAGE__.txt',
'another/__next.another.txt',
'another/first/__next._full.txt',
'another/first/__next._head.txt',
'another/first/__next._index.txt',
'another/first/__next._tree.txt',
'another/first/__next.another.$d$slug.__PAGE__.txt',
......@@ -69,6 +73,7 @@ export const expectedWhenTrailingSlashTrue = [
'another/index.html',
'another/index.txt',
'another/second/__next._full.txt',
'another/second/__next._head.txt',
'another/second/__next._index.txt',
'another/second/__next._tree.txt',
'another/second/__next.another.$d$slug.__PAGE__.txt',
......@@ -79,6 +84,7 @@ export const expectedWhenTrailingSlashTrue = [
'api/json',
'api/txt',
'client/__next._full.txt',
'client/__next._head.txt',
'client/__next._index.txt',
'client/__next._tree.txt',
'client/__next.client.__PAGE__.txt',
......@@ -87,6 +93,7 @@ export const expectedWhenTrailingSlashTrue = [
'client/index.txt',
'favicon.ico',
'image-import/__next._full.txt',
'image-import/__next._head.txt',
'image-import/__next._index.txt',
'image-import/__next._tree.txt',
'image-import/__next.image-import.__PAGE__.txt',
......@@ -102,6 +109,7 @@ const expectedWhenTrailingSlashFalse = [
'404.html',
'__next.__PAGE__.txt',
'__next._full.txt',
'__next._head.txt',
'__next._index.txt',
'__next._tree.txt',
// Turbopack will output favicon in the _next/static/media folder
......@@ -117,6 +125,7 @@ const expectedWhenTrailingSlashFalse = [
'_not-found.html',
'_not-found.txt',
'_not-found/__next._full.txt',
'_not-found/__next._head.txt',
'_not-found/__next._index.txt',
'_not-found/__next._not-found.__PAGE__.txt',
'_not-found/__next._not-found.txt',
......@@ -124,6 +133,7 @@ const expectedWhenTrailingSlashFalse = [
'another.html',
'another.txt',
'another/__next._full.txt',
'another/__next._head.txt',
'another/__next._index.txt',
'another/__next._tree.txt',
'another/__next.another.__PAGE__.txt',
......@@ -131,6 +141,7 @@ const expectedWhenTrailingSlashFalse = [
'another/first.html',
'another/first.txt',
'another/first/__next._full.txt',
'another/first/__next._head.txt',
'another/first/__next._index.txt',
'another/first/__next._tree.txt',
'another/first/__next.another.$d$slug.__PAGE__.txt',
......@@ -139,6 +150,7 @@ const expectedWhenTrailingSlashFalse = [
'another/second.html',
'another/second.txt',
'another/second/__next._full.txt',
'another/second/__next._head.txt',
'another/second/__next._index.txt',
'another/second/__next._tree.txt',
'another/second/__next.another.$d$slug.__PAGE__.txt',
......@@ -149,6 +161,7 @@ const expectedWhenTrailingSlashFalse = [
'client.html',
'client.txt',
'client/__next._full.txt',
'client/__next._head.txt',
'client/__next._index.txt',
'client/__next._tree.txt',
'client/__next.client.__PAGE__.txt',
......@@ -157,6 +170,7 @@ const expectedWhenTrailingSlashFalse = [
'image-import.html',
'image-import.txt',
'image-import/__next._full.txt',
'image-import/__next._head.txt',
'image-import/__next._index.txt',
'image-import/__next._tree.txt',
'image-import/__next.image-import.__PAGE__.txt',
......
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