Commit ea3e129a authored by Jimmy Lai's avatar Jimmy Lai

Dedup fallback segment prefetches after hydration

parent 45ccb41b
...@@ -2,7 +2,13 @@ import { ...@@ -2,7 +2,13 @@ import {
HEAD_REQUEST_KEY, HEAD_REQUEST_KEY,
type SegmentRequestKey, type SegmentRequestKey,
} from '../../../shared/lib/segment-cache/segment-value-encoding' } 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', () => { describe('getOutputExportSegmentRequestUrl', () => {
it('uses the concrete rendered route when no fallback base path is known', () => { it('uses the concrete rendered route when no fallback base path is known', () => {
...@@ -41,3 +47,60 @@ describe('getOutputExportSegmentRequestUrl', () => { ...@@ -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( ...@@ -770,7 +770,8 @@ function deprecated_createOptimisticRouteTree(
export function readOrCreateSegmentCacheEntry( export function readOrCreateSegmentCacheEntry(
now: number, now: number,
fetchStrategy: FetchStrategy, fetchStrategy: FetchStrategy,
tree: RouteTree tree: RouteTree,
outputExportFallbackBasePath: string | null = null
): SegmentCacheEntry { ): SegmentCacheEntry {
const existingEntry = readSegmentCacheEntry(now, tree.varyPath) const existingEntry = readSegmentCacheEntry(now, tree.varyPath)
if (existingEntry !== null) { if (existingEntry !== null) {
...@@ -779,7 +780,11 @@ export function readOrCreateSegmentCacheEntry( ...@@ -779,7 +780,11 @@ export function readOrCreateSegmentCacheEntry(
// Create a pending entry and add it to the cache. The stale time is set to a // 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 // default value; the actual stale time will be set when the entry is
// fulfilled with data from the server response. // fulfilled with data from the server response.
const varyPathForRequest = getSegmentVaryPathForRequest(fetchStrategy, tree) const varyPathForRequest = getSegmentVaryPathForRequest(
fetchStrategy,
tree,
outputExportFallbackBasePath
)
const pendingEntry = createDetachedSegmentCacheEntry(now) const pendingEntry = createDetachedSegmentCacheEntry(now)
const isRevalidation = false const isRevalidation = false
setInCacheMap( setInCacheMap(
...@@ -794,7 +799,8 @@ export function readOrCreateSegmentCacheEntry( ...@@ -794,7 +799,8 @@ export function readOrCreateSegmentCacheEntry(
export function readOrCreateRevalidatingSegmentEntry( export function readOrCreateRevalidatingSegmentEntry(
now: number, now: number,
fetchStrategy: FetchStrategy, fetchStrategy: FetchStrategy,
tree: RouteTree tree: RouteTree,
outputExportFallbackBasePath: string | null = null
): SegmentCacheEntry { ): SegmentCacheEntry {
// This function is called when we've already confirmed that a particular // 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 // segment is cached, but we want to perform another request anyway in case it
...@@ -830,7 +836,11 @@ export function readOrCreateRevalidatingSegmentEntry( ...@@ -830,7 +836,11 @@ export function readOrCreateRevalidatingSegmentEntry(
// Create a pending entry and add it to the cache. The stale time is set to a // 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 // default value; the actual stale time will be set when the entry is
// fulfilled with data from the server response. // fulfilled with data from the server response.
const varyPathForRequest = getSegmentVaryPathForRequest(fetchStrategy, tree) const varyPathForRequest = getSegmentVaryPathForRequest(
fetchStrategy,
tree,
outputExportFallbackBasePath
)
const pendingEntry = createDetachedSegmentCacheEntry(now) const pendingEntry = createDetachedSegmentCacheEntry(now)
const isRevalidation = true const isRevalidation = true
setInCacheMap( setInCacheMap(
...@@ -2316,8 +2326,16 @@ export async function fetchSegmentsOnCacheMiss( ...@@ -2316,8 +2326,16 @@ export async function fetchSegmentsOnCacheMiss(
// generic path. Otherwise use the request vary path. // generic path. Otherwise use the request vary path.
const canonicalVaryPath = const canonicalVaryPath =
process.env.__NEXT_VARY_PARAMS && data.varyParams !== null process.env.__NEXT_VARY_PARAMS && data.varyParams !== null
? getFulfilledSegmentVaryPath(node.tree.varyPath, data.varyParams) ? getFulfilledSegmentVaryPath(
: getSegmentVaryPathForRequest(FetchStrategy.PPR, node.tree) node.tree.varyPath,
data.varyParams,
route.outputExportFallbackBasePath !== null
)
: getSegmentVaryPathForRequest(
FetchStrategy.PPR,
node.tree,
route.outputExportFallbackBasePath
)
let fulfilled: FulfilledSegmentCacheEntry | null = null let fulfilled: FulfilledSegmentCacheEntry | null = null
const nodeEntry = node.entry const nodeEntry = node.entry
......
...@@ -908,7 +908,8 @@ function pingStaticHead( ...@@ -908,7 +908,8 @@ function pingStaticHead(
entry: readOrCreateSegmentCacheEntry( entry: readOrCreateSegmentCacheEntry(
now, now,
FetchStrategy.PPR, FetchStrategy.PPR,
route.metadata route.metadata,
route.outputExportFallbackBasePath
), ),
parent: null, parent: null,
} }
...@@ -1268,7 +1269,12 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary( ...@@ -1268,7 +1269,12 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
let refetchMarker: 'refetch' | 'inside-shared-layout' | null = let refetchMarker: 'refetch' | 'inside-shared-layout' | null =
refetchMarkerContext === null ? '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) { switch (segment.status) {
case EntryStatus.Empty: { case EntryStatus.Empty: {
// This segment is not cached. Add a refetch marker so the server knows // This segment is not cached. Add a refetch marker so the server knows
...@@ -1378,7 +1384,8 @@ function pingRouteTreeAndIncludeDynamicData( ...@@ -1378,7 +1384,8 @@ function pingRouteTreeAndIncludeDynamicData(
// always use runtime prefetching (via `export const prefetch`), and those should check for // always use runtime prefetching (via `export const prefetch`), and those should check for
// entries that include search params. // entries that include search params.
fetchStrategy, fetchStrategy,
tree tree,
route.outputExportFallbackBasePath
) )
let spawnedSegment: PendingSegmentCacheEntry | null = null let spawnedSegment: PendingSegmentCacheEntry | null = null
...@@ -1427,7 +1434,12 @@ function pingRouteTreeAndIncludeDynamicData( ...@@ -1427,7 +1434,12 @@ function pingRouteTreeAndIncludeDynamicData(
break break
} }
} }
spawnedSegment = pingFullSegmentRevalidation(now, tree, fetchStrategy) spawnedSegment = pingFullSegmentRevalidation(
now,
route,
tree,
fetchStrategy
)
} }
break break
} }
...@@ -1441,7 +1453,12 @@ function pingRouteTreeAndIncludeDynamicData( ...@@ -1441,7 +1453,12 @@ function pingRouteTreeAndIncludeDynamicData(
fetchStrategy fetchStrategy
) )
) { ) {
spawnedSegment = pingFullSegmentRevalidation(now, tree, fetchStrategy) spawnedSegment = pingFullSegmentRevalidation(
now,
route,
tree,
fetchStrategy
)
} }
break break
} }
...@@ -1661,7 +1678,12 @@ function accumulateSegmentBundle( ...@@ -1661,7 +1678,12 @@ function accumulateSegmentBundle(
} }
} }
const segment = readOrCreateSegmentCacheEntry(now, task.fetchStrategy, tree) const segment = readOrCreateSegmentCacheEntry(
now,
task.fetchStrategy,
tree,
route.outputExportFallbackBasePath
)
if ( if (
process.env.__NEXT_PREFETCH_INLINING && process.env.__NEXT_PREFETCH_INLINING &&
...@@ -1687,7 +1709,8 @@ function accumulateSegmentBundle( ...@@ -1687,7 +1709,8 @@ function accumulateSegmentBundle(
entry: readOrCreateSegmentCacheEntry( entry: readOrCreateSegmentCacheEntry(
now, now,
FetchStrategy.PPR, FetchStrategy.PPR,
route.metadata route.metadata,
route.outputExportFallbackBasePath
), ),
parent: parentBundle, parent: parentBundle,
} }
...@@ -1727,13 +1750,15 @@ function finishStaticBundleOnRuntimeBailout( ...@@ -1727,13 +1750,15 @@ function finishStaticBundleOnRuntimeBailout(
function pingFullSegmentRevalidation( function pingFullSegmentRevalidation(
now: number, now: number,
route: FulfilledRouteCacheEntry,
tree: RouteTree, tree: RouteTree,
fetchStrategy: FetchStrategy.Full | FetchStrategy.PPRRuntime fetchStrategy: FetchStrategy.Full | FetchStrategy.PPRRuntime
): PendingSegmentCacheEntry | null { ): PendingSegmentCacheEntry | null {
const revalidatingSegment = readOrCreateRevalidatingSegmentEntry( const revalidatingSegment = readOrCreateRevalidatingSegmentEntry(
now, now,
fetchStrategy, fetchStrategy,
tree tree,
route.outputExportFallbackBasePath
) )
if (revalidatingSegment.status === EntryStatus.Empty) { if (revalidatingSegment.status === EntryStatus.Empty) {
// During a Full/PPRRuntime prefetch, a single dynamic request is made for all the // 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( ...@@ -244,7 +244,8 @@ export function finalizeMetadataVaryPath(
export function getSegmentVaryPathForRequest( export function getSegmentVaryPathForRequest(
fetchStrategy: FetchStrategy, fetchStrategy: FetchStrategy,
tree: RouteTree tree: RouteTree,
outputExportFallbackBasePath: string | null = null
): SegmentVaryPath { ): SegmentVaryPath {
// This is used for storing pending requests in the cache. We want to choose // 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. // the most generic vary path based on the strategy used to fetch it, i.e.
...@@ -272,6 +273,15 @@ export function getSegmentVaryPathForRequest( ...@@ -272,6 +273,15 @@ export function getSegmentVaryPathForRequest(
// Only page segments (and the special "metadata" segment, which is treated // Only page segments (and the special "metadata" segment, which is treated
// like a page segment for the purposes of caching) may contain search // 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. // 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) { if (tree.isPage) {
// Only a runtime prefetch will include search params in the vary path. // Only a runtime prefetch will include search params in the vary path.
// Static prefetches never include search params, so they can be reused // Static prefetches never include search params, so they can be reused
...@@ -287,8 +297,6 @@ export function getSegmentVaryPathForRequest( ...@@ -287,8 +297,6 @@ export function getSegmentVaryPathForRequest(
// //
// requestKey -> searchParams -> pathParams // requestKey -> searchParams -> pathParams
// ^ This part gets replaced with Fallback // ^ This part gets replaced with Fallback
const searchParamsVaryPath = (originalVaryPath as PageVaryPath).parent
const pathParamsVaryPath = searchParamsVaryPath.parent
const patchedVaryPath: VaryPath = { const patchedVaryPath: VaryPath = {
id: null, id: null,
value: originalVaryPath.value, value: originalVaryPath.value,
...@@ -300,12 +308,53 @@ export function getSegmentVaryPathForRequest( ...@@ -300,12 +308,53 @@ export function getSegmentVaryPathForRequest(
} }
return patchedVaryPath as SegmentVaryPath 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. // The request does vary on search params. We don't need to modify anything.
return originalVaryPath as SegmentVaryPath 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( export function clonePageVaryPathWithNewSearchParams(
originalVaryPath: PageVaryPath, originalVaryPath: PageVaryPath,
newSearch: NormalizedSearch newSearch: NormalizedSearch
...@@ -336,7 +385,8 @@ export function getRenderedSearchFromVaryPath( ...@@ -336,7 +385,8 @@ export function getRenderedSearchFromVaryPath(
export function getFulfilledSegmentVaryPath( export function getFulfilledSegmentVaryPath(
original: VaryPath, original: VaryPath,
varyParams: Set<string> varyParams: Set<string>,
forceFallbackPathParams: boolean = false
): SegmentVaryPath { ): SegmentVaryPath {
// Re-keys a segment's vary path based on which params the segment actually // 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 // depends on. Params that are NOT in the varyParams set are replaced with
...@@ -348,17 +398,26 @@ export function getFulfilledSegmentVaryPath( ...@@ -348,17 +398,26 @@ export function getFulfilledSegmentVaryPath(
// accessed during rendering. // accessed during rendering.
const clone: VaryPath = { const clone: VaryPath = {
id: original.id, 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 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. // so always preserve it. For output export fallback routes, path params
// Otherwise, replace with Fallback to make it reusable. // are always generic even if the server reports them as accessed, because
// the emitted fallback artifacts are shared across all param values.
value: value:
original.id === null || varyParams.has(original.id) original.id === null
? original.value ? original.value
: Fallback, : forceFallbackPathParams && original.id !== '?'
? Fallback
: varyParams.has(original.id)
? original.value
: Fallback,
parent: parent:
original.parent === null original.parent === null
? null ? null
: getFulfilledSegmentVaryPath(original.parent, varyParams), : getFulfilledSegmentVaryPath(
original.parent,
varyParams,
forceFallbackPathParams
),
} }
return clone as SegmentVaryPath return clone as SegmentVaryPath
} }
...@@ -14,6 +14,11 @@ export default function HydratedThreadPage() { ...@@ -14,6 +14,11 @@ export default function HydratedThreadPage() {
Visit hydrated thread second Visit hydrated thread second
</LinkAccordion> </LinkAccordion>
</li> </li>
<li>
<LinkAccordion href="/hydrated/third">
Visit hydrated thread third
</LinkAccordion>
</li>
</ul> </ul>
</> </>
) )
......
...@@ -423,6 +423,7 @@ if (skipped) { ...@@ -423,6 +423,7 @@ if (skipped) {
expect(await browser.elementByCss('h1').text()).toBe('first') expect(await browser.elementByCss('h1').text()).toBe('first')
}) })
await act(async () => {})
clearRequests() clearRequests()
await act(async () => { await act(async () => {
...@@ -459,6 +460,49 @@ if (skipped) { ...@@ -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 () => { it('generates a hidden _fallback.html bootstrap document', async () => {
const outDir = join(next.testDir, 'out') const outDir = join(next.testDir, 'out')
const fallbackHtml = await fs.readFile( 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