Commit 3713e8ac authored by Jimmy Lai's avatar Jimmy Lai

Reuse fallback segment base for optimistic routes

parent 9553f4cb
import {
HEAD_REQUEST_KEY,
type SegmentRequestKey,
} from '../../../shared/lib/segment-cache/segment-value-encoding'
import { getOutputExportSegmentRequestUrl } from './cache'
describe('getOutputExportSegmentRequestUrl', () => {
it('uses the concrete rendered route when no fallback base path is known', () => {
expect(
getOutputExportSegmentRequestUrl(
new URL('https://example.com/org/acme/chat/thread-456/?mode=full'),
HEAD_REQUEST_KEY,
null
).href
).toBe(
'https://example.com/org/acme/chat/thread-456/__next._head.txt?mode=full'
)
})
it('reuses the learned fallback base path for sibling segment requests', () => {
expect(
getOutputExportSegmentRequestUrl(
new URL('https://example.com/org/acme/chat/thread-456/?mode=full'),
'/t/$d$threadId' as SegmentRequestKey,
'/org/__fallback'
).href
).toBe(
'https://example.com/org/__fallback/__next.t.$d$threadId.txt?mode=full'
)
})
it('keeps the matched branch fallback base path for conflicting routes', () => {
expect(
getOutputExportSegmentRequestUrl(
new URL('https://example.com/docs/api/reference'),
'/docs/$d$section/$d$page' as SegmentRequestKey,
'/docs/__fallback/__route_0'
).href
).toBe(
'https://example.com/docs/__fallback/__route_0/__next.docs.$d$section.$d$page.txt'
)
})
})
...@@ -230,6 +230,7 @@ export type FulfilledRouteCacheEntry = RouteCacheEntryShared & { ...@@ -230,6 +230,7 @@ export type FulfilledRouteCacheEntry = RouteCacheEntryShared & {
metadata: RouteTree metadata: RouteTree
supportsPerSegmentPrefetching: boolean supportsPerSegmentPrefetching: boolean
hasInlinedSegments: boolean hasInlinedSegments: boolean
outputExportFallbackBasePath: string | null
// When true, this entry should not be used as a template for route // When true, this entry should not be used as a template for route
// prediction. Set when we discover that the URL was rewritten by middleware // prediction. Set when we discover that the URL was rewritten by middleware
// to a different route structure (e.g., /foo was rewritten to /bar). Since // to a different route structure (e.g., /foo was rewritten to /bar). Since
...@@ -695,6 +696,8 @@ export function deprecated_requestOptimisticRouteCacheEntry( ...@@ -695,6 +696,8 @@ export function deprecated_requestOptimisticRouteCacheEntry(
supportsPerSegmentPrefetching: supportsPerSegmentPrefetching:
routeWithNoSearchParams.supportsPerSegmentPrefetching, routeWithNoSearchParams.supportsPerSegmentPrefetching,
hasInlinedSegments: routeWithNoSearchParams.hasInlinedSegments, hasInlinedSegments: routeWithNoSearchParams.hasInlinedSegments,
outputExportFallbackBasePath:
routeWithNoSearchParams.outputExportFallbackBasePath,
hasDynamicRewrite: routeWithNoSearchParams.hasDynamicRewrite, hasDynamicRewrite: routeWithNoSearchParams.hasDynamicRewrite,
// Override the rendered search with the optimistic value. // Override the rendered search with the optimistic value.
...@@ -1116,6 +1119,7 @@ export function fulfillRouteCacheEntry( ...@@ -1116,6 +1119,7 @@ export function fulfillRouteCacheEntry(
fulfilledEntry.renderedSearch = renderedSearch fulfilledEntry.renderedSearch = renderedSearch
fulfilledEntry.supportsPerSegmentPrefetching = supportsPerSegmentPrefetching fulfilledEntry.supportsPerSegmentPrefetching = supportsPerSegmentPrefetching
fulfilledEntry.hasInlinedSegments = false fulfilledEntry.hasInlinedSegments = false
fulfilledEntry.outputExportFallbackBasePath = null
fulfilledEntry.hasDynamicRewrite = false fulfilledEntry.hasDynamicRewrite = false
pingBlockedTasks(entry) pingBlockedTasks(entry)
return fulfilledEntry return fulfilledEntry
...@@ -1912,6 +1916,7 @@ type OutputExportFallbackNavigationData = { ...@@ -1912,6 +1916,7 @@ type OutputExportFallbackNavigationData = {
couldBeIntercepted: boolean couldBeIntercepted: boolean
flightDatas: NormalizedFlightData[] flightDatas: NormalizedFlightData[]
headVaryParams: VaryParams | null headVaryParams: VaryParams | null
outputExportFallbackBasePath: string
responseSize: number responseSize: number
staleAt: number staleAt: number
supportsPerSegmentPrefetching: boolean supportsPerSegmentPrefetching: boolean
...@@ -1977,6 +1982,7 @@ async function fetchOutputExportFallbackNavigationData( ...@@ -1977,6 +1982,7 @@ async function fetchOutputExportFallbackNavigationData(
couldBeIntercepted: serverData.i, couldBeIntercepted: serverData.i,
flightDatas, flightDatas,
headVaryParams, headVaryParams,
outputExportFallbackBasePath: fallbackResult.fallbackUrl.pathname,
responseSize, responseSize,
staleAt: await getStaleAt(now, serverData.s), staleAt: await getStaleAt(now, serverData.s),
supportsPerSegmentPrefetching: serverData.S, supportsPerSegmentPrefetching: serverData.S,
...@@ -2007,6 +2013,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback( ...@@ -2007,6 +2013,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
couldBeIntercepted, couldBeIntercepted,
flightDatas, flightDatas,
headVaryParams, headVaryParams,
outputExportFallbackBasePath,
responseSize, responseSize,
staleAt, staleAt,
supportsPerSegmentPrefetching, supportsPerSegmentPrefetching,
...@@ -2054,6 +2061,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback( ...@@ -2054,6 +2061,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
false false
) )
fulfilledEntry.hasInlinedSegments = true fulfilledEntry.hasInlinedSegments = true
fulfilledEntry.outputExportFallbackBasePath = outputExportFallbackBasePath
if (!couldBeIntercepted) { if (!couldBeIntercepted) {
const fulfilledVaryPath = getFulfilledRouteVaryPath( const fulfilledVaryPath = getFulfilledRouteVaryPath(
...@@ -2200,10 +2208,14 @@ export async function fetchSegmentsOnCacheMiss( ...@@ -2200,10 +2208,14 @@ export async function fetchSegmentsOnCacheMiss(
// Testing API. Static pre-renders don't normally happen during development. // Testing API. Static pre-renders don't normally happen during development.
addInstantPrefetchHeaderIfLocked(headers) addInstantPrefetchHeaderIfLocked(headers)
const requestUrl = isOutputExportMode let requestUrl = url
? // In output: "export" mode, we need to add the segment path to the URL. if (isOutputExportMode) {
addSegmentPathToUrlInOutputExportMode(url, normalizedRequestKey) requestUrl = getOutputExportSegmentRequestUrl(
: url url,
normalizedRequestKey,
route.outputExportFallbackBasePath
)
}
try { try {
const response = await fetchPrefetchResponse(requestUrl, headers) const response = await fetchPrefetchResponse(requestUrl, headers)
if ( if (
...@@ -3100,14 +3112,15 @@ function createIncrementalPrefetchResponseStream( ...@@ -3100,14 +3112,15 @@ function createIncrementalPrefetchResponseStream(
}) })
} }
function addSegmentPathToUrlInOutputExportMode( export function getOutputExportSegmentRequestUrl(
url: URL, url: URL,
segmentPath: SegmentRequestKey segmentPath: SegmentRequestKey,
outputExportFallbackBasePath: string | null
): URL { ): URL {
if (isOutputExportMode) {
// In output: "export" mode, we cannot use a header to encode the segment
// path. Instead, we append it to the end of the pathname.
const staticUrl = new URL(url) const staticUrl = new URL(url)
if (outputExportFallbackBasePath !== null) {
staticUrl.pathname = outputExportFallbackBasePath
}
const routeDir = staticUrl.pathname.endsWith('/') const routeDir = staticUrl.pathname.endsWith('/')
? staticUrl.pathname.slice(0, -1) ? staticUrl.pathname.slice(0, -1)
: staticUrl.pathname : staticUrl.pathname
...@@ -3115,6 +3128,16 @@ function addSegmentPathToUrlInOutputExportMode( ...@@ -3115,6 +3128,16 @@ function addSegmentPathToUrlInOutputExportMode(
convertSegmentPathToStaticExportFilename(segmentPath) convertSegmentPathToStaticExportFilename(segmentPath)
staticUrl.pathname = `${routeDir}/${staticExportFilename}` staticUrl.pathname = `${routeDir}/${staticExportFilename}`
return staticUrl return staticUrl
}
function addSegmentPathToUrlInOutputExportMode(
url: URL,
segmentPath: SegmentRequestKey
): URL {
if (isOutputExportMode) {
// In output: "export" mode, we cannot use a header to encode the segment
// path. Instead, we append it to the end of the pathname.
return getOutputExportSegmentRequestUrl(url, segmentPath, null)
} }
return url return url
} }
......
...@@ -588,6 +588,7 @@ export function matchKnownRoute( ...@@ -588,6 +588,7 @@ export function matchKnownRoute(
couldBeIntercepted: pattern.couldBeIntercepted, couldBeIntercepted: pattern.couldBeIntercepted,
supportsPerSegmentPrefetching: pattern.supportsPerSegmentPrefetching, supportsPerSegmentPrefetching: pattern.supportsPerSegmentPrefetching,
hasInlinedSegments: pattern.hasInlinedSegments, hasInlinedSegments: pattern.hasInlinedSegments,
outputExportFallbackBasePath: pattern.outputExportFallbackBasePath,
hasDynamicRewrite: false, hasDynamicRewrite: false,
renderedSearch: search, renderedSearch: search,
ref: null, ref: null,
......
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