Commit ea3e129a authored by Jimmy Lai's avatar Jimmy Lai

Dedup fallback segment prefetches after hydration

parent 45ccb41b
......@@ -2,7 +2,13 @@ import {
HEAD_REQUEST_KEY,
type SegmentRequestKey,
} from '../../../shared/lib/segment-cache/segment-value-encoding'
import { getOutputExportSegmentRequestUrl } from './cache'
import { FetchStrategy } from './types'
import { Fallback } from './cache-map'
import {
getOutputExportSegmentRequestUrl,
readOrCreateSegmentCacheEntry,
} from './cache'
import { finalizeMetadataVaryPath } from './vary-path'
describe('getOutputExportSegmentRequestUrl', () => {
it('uses the concrete rendered route when no fallback base path is known', () => {
......@@ -41,3 +47,60 @@ describe('getOutputExportSegmentRequestUrl', () => {
)
})
})
describe('readOrCreateSegmentCacheEntry output export fallback', () => {
it('dedupes pending metadata entries across sibling fallback params', () => {
const now = Date.now()
const firstTree = {
requestKey: HEAD_REQUEST_KEY,
segment: HEAD_REQUEST_KEY,
refreshState: null,
varyPath: finalizeMetadataVaryPath(
'/hydrated/$d$thread/__PAGE__' as SegmentRequestKey,
'' as any,
{
id: 'thread',
value: 'first' as any,
parent: null,
} as any
),
isPage: true as const,
slots: null,
prefetchHints: 0,
}
const secondTree = {
...firstTree,
varyPath: finalizeMetadataVaryPath(
'/hydrated/$d$thread/__PAGE__' as SegmentRequestKey,
'' as any,
{
id: 'thread',
value: 'second' as any,
parent: null,
} as any
),
}
const firstEntry = readOrCreateSegmentCacheEntry(
now,
FetchStrategy.PPR,
firstTree,
'/hydrated/__fallback'
)
const secondEntry = readOrCreateSegmentCacheEntry(
now,
FetchStrategy.PPR,
secondTree,
'/hydrated/__fallback'
)
expect(firstEntry).toBe(secondEntry)
expect(secondTree.varyPath.parent.parent.value).toBe('second')
expect(
(
secondTree.varyPath.parent
.parent as typeof secondTree.varyPath.parent.parent
).value
).not.toBe(Fallback)
})
})
......@@ -770,7 +770,8 @@ function deprecated_createOptimisticRouteTree(
export function readOrCreateSegmentCacheEntry(
now: number,
fetchStrategy: FetchStrategy,
tree: RouteTree
tree: RouteTree,
outputExportFallbackBasePath: string | null = null
): SegmentCacheEntry {
const existingEntry = readSegmentCacheEntry(now, tree.varyPath)
if (existingEntry !== null) {
......@@ -779,7 +780,11 @@ export function readOrCreateSegmentCacheEntry(
// Create a pending entry and add it to the cache. The stale time is set to a
// default value; the actual stale time will be set when the entry is
// fulfilled with data from the server response.
const varyPathForRequest = getSegmentVaryPathForRequest(fetchStrategy, tree)
const varyPathForRequest = getSegmentVaryPathForRequest(
fetchStrategy,
tree,
outputExportFallbackBasePath
)
const pendingEntry = createDetachedSegmentCacheEntry(now)
const isRevalidation = false
setInCacheMap(
......@@ -794,7 +799,8 @@ export function readOrCreateSegmentCacheEntry(
export function readOrCreateRevalidatingSegmentEntry(
now: number,
fetchStrategy: FetchStrategy,
tree: RouteTree
tree: RouteTree,
outputExportFallbackBasePath: string | null = null
): SegmentCacheEntry {
// This function is called when we've already confirmed that a particular
// segment is cached, but we want to perform another request anyway in case it
......@@ -830,7 +836,11 @@ export function readOrCreateRevalidatingSegmentEntry(
// Create a pending entry and add it to the cache. The stale time is set to a
// default value; the actual stale time will be set when the entry is
// fulfilled with data from the server response.
const varyPathForRequest = getSegmentVaryPathForRequest(fetchStrategy, tree)
const varyPathForRequest = getSegmentVaryPathForRequest(
fetchStrategy,
tree,
outputExportFallbackBasePath
)
const pendingEntry = createDetachedSegmentCacheEntry(now)
const isRevalidation = true
setInCacheMap(
......@@ -2316,8 +2326,16 @@ export async function fetchSegmentsOnCacheMiss(
// generic path. Otherwise use the request vary path.
const canonicalVaryPath =
process.env.__NEXT_VARY_PARAMS && data.varyParams !== null
? getFulfilledSegmentVaryPath(node.tree.varyPath, data.varyParams)
: getSegmentVaryPathForRequest(FetchStrategy.PPR, node.tree)
? getFulfilledSegmentVaryPath(
node.tree.varyPath,
data.varyParams,
route.outputExportFallbackBasePath !== null
)
: getSegmentVaryPathForRequest(
FetchStrategy.PPR,
node.tree,
route.outputExportFallbackBasePath
)
let fulfilled: FulfilledSegmentCacheEntry | null = null
const nodeEntry = node.entry
......
......@@ -908,7 +908,8 @@ function pingStaticHead(
entry: readOrCreateSegmentCacheEntry(
now,
FetchStrategy.PPR,
route.metadata
route.metadata,
route.outputExportFallbackBasePath
),
parent: null,
}
......@@ -1268,7 +1269,12 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
let refetchMarker: 'refetch' | 'inside-shared-layout' | null =
refetchMarkerContext === null ? 'inside-shared-layout' : null
const segment = readOrCreateSegmentCacheEntry(now, task.fetchStrategy, tree)
const segment = readOrCreateSegmentCacheEntry(
now,
task.fetchStrategy,
tree,
route.outputExportFallbackBasePath
)
switch (segment.status) {
case EntryStatus.Empty: {
// This segment is not cached. Add a refetch marker so the server knows
......@@ -1378,7 +1384,8 @@ function pingRouteTreeAndIncludeDynamicData(
// always use runtime prefetching (via `export const prefetch`), and those should check for
// entries that include search params.
fetchStrategy,
tree
tree,
route.outputExportFallbackBasePath
)
let spawnedSegment: PendingSegmentCacheEntry | null = null
......@@ -1427,7 +1434,12 @@ function pingRouteTreeAndIncludeDynamicData(
break
}
}
spawnedSegment = pingFullSegmentRevalidation(now, tree, fetchStrategy)
spawnedSegment = pingFullSegmentRevalidation(
now,
route,
tree,
fetchStrategy
)
}
break
}
......@@ -1441,7 +1453,12 @@ function pingRouteTreeAndIncludeDynamicData(
fetchStrategy
)
) {
spawnedSegment = pingFullSegmentRevalidation(now, tree, fetchStrategy)
spawnedSegment = pingFullSegmentRevalidation(
now,
route,
tree,
fetchStrategy
)
}
break
}
......@@ -1661,7 +1678,12 @@ function accumulateSegmentBundle(
}
}
const segment = readOrCreateSegmentCacheEntry(now, task.fetchStrategy, tree)
const segment = readOrCreateSegmentCacheEntry(
now,
task.fetchStrategy,
tree,
route.outputExportFallbackBasePath
)
if (
process.env.__NEXT_PREFETCH_INLINING &&
......@@ -1687,7 +1709,8 @@ function accumulateSegmentBundle(
entry: readOrCreateSegmentCacheEntry(
now,
FetchStrategy.PPR,
route.metadata
route.metadata,
route.outputExportFallbackBasePath
),
parent: parentBundle,
}
......@@ -1727,13 +1750,15 @@ function finishStaticBundleOnRuntimeBailout(
function pingFullSegmentRevalidation(
now: number,
route: FulfilledRouteCacheEntry,
tree: RouteTree,
fetchStrategy: FetchStrategy.Full | FetchStrategy.PPRRuntime
): PendingSegmentCacheEntry | null {
const revalidatingSegment = readOrCreateRevalidatingSegmentEntry(
now,
fetchStrategy,
tree
tree,
route.outputExportFallbackBasePath
)
if (revalidatingSegment.status === EntryStatus.Empty) {
// During a Full/PPRRuntime prefetch, a single dynamic request is made for all the
......
import { Fallback } from './cache-map'
import { FetchStrategy } from './types'
import {
finalizeMetadataVaryPath,
finalizePageVaryPath,
getFulfilledSegmentVaryPath,
getSegmentVaryPathForRequest,
} from './vary-path'
describe('getSegmentVaryPathForRequest output export fallback', () => {
it('reuses path params for normal static prefetches', () => {
const varyPath = finalizeMetadataVaryPath(
'/t/$d$threadId/__PAGE__',
'' as any,
{
id: 'threadId',
value: 'j97358' as any,
parent: null,
} as any
)
const requestVaryPath = getSegmentVaryPathForRequest(
FetchStrategy.PPR,
{
requestKey: '/_head',
segment: '/_head',
refreshState: null,
varyPath,
isPage: true,
slots: null,
prefetchHints: 0,
},
null
)
expect(requestVaryPath.parent.parent.value).toBe('j97358')
})
it('treats path params as reusable for learned output export fallback routes', () => {
const varyPath = finalizePageVaryPath(
'/t/$d$threadId/__PAGE__',
'' as any,
{
id: 'threadId',
value: 'j97358' as any,
parent: null,
} as any
)
const requestVaryPath = getSegmentVaryPathForRequest(
FetchStrategy.PPR,
{
requestKey: '/t/$d$threadId/__PAGE__',
segment: '/t/$d$threadId',
refreshState: null,
varyPath,
isPage: true,
slots: null,
prefetchHints: 0,
},
'/t/__fallback'
)
expect(requestVaryPath.parent.value).toBe(Fallback)
expect(requestVaryPath.parent.parent.value).toBe(Fallback)
})
it('keeps fulfilled fallback segment path params generic even when vary params include them', () => {
const varyPath = finalizeMetadataVaryPath(
'/t/$d$threadId/__PAGE__',
'' as any,
{
id: 'threadId',
value: 'j97358' as any,
parent: null,
} as any
)
const fulfilledVaryPath = getFulfilledSegmentVaryPath(
varyPath,
new Set(['?', 'threadId']),
true
)
expect(fulfilledVaryPath.parent.value).toBe('')
expect(fulfilledVaryPath.parent.parent.value).toBe(Fallback)
})
})
......@@ -244,7 +244,8 @@ export function finalizeMetadataVaryPath(
export function getSegmentVaryPathForRequest(
fetchStrategy: FetchStrategy,
tree: RouteTree
tree: RouteTree,
outputExportFallbackBasePath: string | null = null
): 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.
......@@ -272,6 +273,15 @@ export function getSegmentVaryPathForRequest(
// 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.
const originalPathParamsVaryPath = tree.isPage
? getPartialPageVaryPath(originalVaryPath as PageVaryPath)
: getPartialLayoutVaryPath(originalVaryPath as LayoutVaryPath)
const pathParamsVaryPath =
outputExportFallbackBasePath !== null
? clonePathParamsVaryPathWithFallback(originalPathParamsVaryPath)
: originalPathParamsVaryPath
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
......@@ -287,8 +297,6 @@ export function getSegmentVaryPathForRequest(
//
// requestKey -> searchParams -> pathParams
// ^ This part gets replaced with Fallback
const searchParamsVaryPath = (originalVaryPath as PageVaryPath).parent
const pathParamsVaryPath = searchParamsVaryPath.parent
const patchedVaryPath: VaryPath = {
id: null,
value: originalVaryPath.value,
......@@ -300,12 +308,53 @@ export function getSegmentVaryPathForRequest(
}
return patchedVaryPath as SegmentVaryPath
}
if (pathParamsVaryPath !== originalPathParamsVaryPath) {
const searchParamsVaryPath = (originalVaryPath as PageVaryPath).parent
const patchedVaryPath: VaryPath = {
id: null,
value: originalVaryPath.value,
parent: {
id: '?',
value: searchParamsVaryPath.value,
parent: pathParamsVaryPath,
},
}
return patchedVaryPath as SegmentVaryPath
}
}
if (pathParamsVaryPath !== originalPathParamsVaryPath) {
const patchedVaryPath: VaryPath = {
id: null,
value: originalVaryPath.value,
parent: pathParamsVaryPath,
}
return patchedVaryPath as SegmentVaryPath
}
// The request does vary on search params. We don't need to modify anything.
return originalVaryPath as SegmentVaryPath
}
function clonePathParamsVaryPathWithFallback(
originalVaryPath: PartialSegmentVaryPath | null
): PartialSegmentVaryPath | null {
if (originalVaryPath === null) {
return null
}
const clonedParent = clonePathParamsVaryPathWithFallback(
originalVaryPath.parent as PartialSegmentVaryPath | null
)
const clonedVaryPath: VaryPath = {
id: originalVaryPath.id,
value: originalVaryPath.id === null ? originalVaryPath.value : Fallback,
parent: clonedParent,
}
return clonedVaryPath as PartialSegmentVaryPath
}
export function clonePageVaryPathWithNewSearchParams(
originalVaryPath: PageVaryPath,
newSearch: NormalizedSearch
......@@ -336,7 +385,8 @@ export function getRenderedSearchFromVaryPath(
export function getFulfilledSegmentVaryPath(
original: VaryPath,
varyParams: Set<string>
varyParams: Set<string>,
forceFallbackPathParams: boolean = false
): SegmentVaryPath {
// Re-keys a segment's vary path based on which params the segment actually
// depends on. Params that are NOT in the varyParams set are replaced with
......@@ -348,17 +398,26 @@ export function getFulfilledSegmentVaryPath(
// accessed during rendering.
const clone: VaryPath = {
id: original.id,
// If the id is null, this node is not a param (e.g., it's a request key).
// If the id is in the varyParams set, keep the original value.
// Otherwise, replace with Fallback to make it reusable.
// If the id is null, this node is not a param (e.g., it's a request key),
// so always preserve it. For output export fallback routes, path params
// are always generic even if the server reports them as accessed, because
// the emitted fallback artifacts are shared across all param values.
value:
original.id === null || varyParams.has(original.id)
original.id === null
? original.value
: Fallback,
: forceFallbackPathParams && original.id !== '?'
? Fallback
: varyParams.has(original.id)
? original.value
: Fallback,
parent:
original.parent === null
? null
: getFulfilledSegmentVaryPath(original.parent, varyParams),
: getFulfilledSegmentVaryPath(
original.parent,
varyParams,
forceFallbackPathParams
),
}
return clone as SegmentVaryPath
}
......@@ -14,6 +14,11 @@ export default function HydratedThreadPage() {
Visit hydrated thread second
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/hydrated/third">
Visit hydrated thread third
</LinkAccordion>
</li>
</ul>
</>
)
......
......@@ -423,6 +423,7 @@ if (skipped) {
expect(await browser.elementByCss('h1').text()).toBe('first')
})
await act(async () => {})
clearRequests()
await act(async () => {
......@@ -459,6 +460,49 @@ if (skipped) {
}
})
it('dedupes hydrated sibling metadata prefetches by fallback artifact path', async () => {
let act!: ReturnType<typeof createRouterAct>
const browser = await webdriver(port, '/hydrated/first/', {
beforePageLoad(page: Playwright.Page) {
act = createRouterAct(page)
},
})
try {
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('first')
})
clearRequests()
await act(async () => {
await browser
.elementByCss('input[data-link-accordion="/hydrated/second"]')
.click()
await browser
.elementByCss('input[data-link-accordion="/hydrated/third"]')
.click()
})
const prefetchRequests = getRequests()
expect(
prefetchRequests.filter((requestPath) =>
requestPath.startsWith('/hydrated/__fallback/__next._head.txt')
)
).toHaveLength(1)
expect(
prefetchRequests.filter((requestPath) =>
requestPath.includes(
'/hydrated/__fallback/__next.hydrated.$d$thread.__PAGE__.txt'
)
)
).toHaveLength(1)
} finally {
await browser.close()
}
})
it('generates a hidden _fallback.html bootstrap document', async () => {
const outDir = join(next.testDir, 'out')
const fallbackHtml = await fs.readFile(
......
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