Commit a1779059 authored by Jimmy Lai's avatar Jimmy Lai

Dedupe output export fallback client fetches

parent 4cd7cad7
......@@ -249,30 +249,36 @@ if (
}
let initialServerResponse: Promise<InitialRSCPayload>
if (instantTestStaticFetch) {
const processedStaticFetch = Promise.resolve(instantTestStaticFetch)
const decodeFallbackPrerenderPayload = async (
responsePromise: Promise<Response>,
renderedUrl?: URL
): Promise<InitialRSCPayload> => {
const processedResponse = responsePromise
.then(processFetch)
.then(({ response }) => response)
// Instant Navigation Testing API: hydrate from the static RSC payload
// fetch kicked off by an injected <script> tag, instead of the inline
// Flight data (which is not present in the static shell).
initialServerResponse = Promise.resolve(
createFromFetch<InitialRSCPayload>(processedStaticFetch, {
const initialRSCPayload = await createFromFetch<InitialRSCPayload>(
processedResponse,
{
callServer,
findSourceMapURL,
debugChannel,
// The static fetch response is a partial stream (static-only Flight
// data with no dynamic content). Allow it to close without error so
// React treats dynamic holes as still-suspended rather than
// triggering error recovery.
unstable_allowPartialStream: true,
})
).then(async (initialRSCPayload) => {
return createInitialRSCPayloadFromFallbackPrerender(
await processedStaticFetch,
initialRSCPayload
)
})
}
)
return createInitialRSCPayloadFromFallbackPrerender(
await processedResponse,
initialRSCPayload,
renderedUrl
)
}
if (instantTestStaticFetch) {
// Instant Navigation Testing API: hydrate from the static RSC payload
// fetch kicked off by an injected <script> tag, instead of the inline
// Flight data (which is not present in the static shell).
initialServerResponse = decodeFallbackPrerenderPayload(
Promise.resolve(instantTestStaticFetch)
)
} else if (window.__NEXT_EXPORT_FALLBACK) {
// This must be checked before __NEXT_CLIENT_RESUME because
// _fallback.html may be based on a PPR shell that also sets
......@@ -288,21 +294,8 @@ if (instantTestStaticFetch) {
)
if (fallbackResult !== null) {
const processedResponse = Promise.resolve(fallbackResult.response)
.then(processFetch)
.then(({ response: processed }) => processed)
const fallbackInitialRSCPayload =
await createFromFetch<InitialRSCPayload>(processedResponse, {
callServer,
findSourceMapURL,
debugChannel,
unstable_allowPartialStream: true,
})
return createInitialRSCPayloadFromFallbackPrerender(
await processedResponse,
fallbackInitialRSCPayload,
return decodeFallbackPrerenderPayload(
Promise.resolve(fallbackResult.response),
renderedUrl
)
}
......@@ -321,23 +314,8 @@ if (instantTestStaticFetch) {
}
))
const processedResponse = Promise.resolve(response)
.then(processFetch)
.then(({ response: processed }) => processed)
const fallbackInitialRSCPayload = await createFromFetch<InitialRSCPayload>(
processedResponse,
{
callServer,
findSourceMapURL,
debugChannel,
unstable_allowPartialStream: true,
}
)
return createInitialRSCPayloadFromFallbackPrerender(
await processedResponse,
fallbackInitialRSCPayload,
return decodeFallbackPrerenderPayload(
Promise.resolve(response),
renderedUrl
)
})()
......@@ -348,24 +326,11 @@ if (instantTestStaticFetch) {
const clientResumeFetch: Promise<Response> =
// @ts-expect-error
window.__NEXT_CLIENT_RESUME
const processedClientResumeFetch = Promise.resolve(clientResumeFetch)
.then(processFetch)
.then(({ response }) => response)
const exportOriginalUrl = outputExportResumeUrl
delete window.__NEXT_EXPORT_ORIGINAL_URL
initialServerResponse = Promise.resolve(
createFromFetch<InitialRSCPayload>(processedClientResumeFetch, {
callServer,
findSourceMapURL,
debugChannel,
unstable_allowPartialStream: true,
})
).then(async (fallbackInitialRSCPayload) =>
createInitialRSCPayloadFromFallbackPrerender(
await processedClientResumeFetch,
fallbackInitialRSCPayload,
exportOriginalUrl
)
initialServerResponse = decodeFallbackPrerenderPayload(
clientResumeFetch,
exportOriginalUrl
)
} else {
initialServerResponse = createFromReadableStream<InitialRSCPayload>(
......
......@@ -1906,26 +1906,33 @@ export async function fetchRouteOnCacheMiss(
}
}
async function fetchRouteOnCacheMissFromOutputExportFallback(
entry: PendingRouteCacheEntry,
key: RouteCacheKey,
headers: RequestHeaders
): Promise<PrefetchSubtaskResult<null> | null> {
const now = Date.now()
const renderedUrl = new URL(key.pathname + key.search, location.origin)
type OutputExportFallbackNavigationData = {
buildId: string
closed: Promise<void>
couldBeIntercepted: boolean
flightDatas: NormalizedFlightData[]
headVaryParams: VaryParams | null
responseSize: number
staleAt: number
supportsPerSegmentPrefetching: boolean
}
async function fetchOutputExportFallbackNavigationData(
renderedUrl: URL,
headers: RequestHeaders,
now: number
): Promise<OutputExportFallbackNavigationData | null> {
const fallbackResult = await fetchOutputExportFallbackResponse(renderedUrl, {
credentials: 'same-origin',
headers,
})
if (fallbackResult === null) {
rejectRouteCacheEntry(entry, now + 10 * 1000)
return null
}
const { response } = await processFetch(fallbackResult.response)
if (!response.body) {
rejectRouteCacheEntry(entry, now + 10 * 1000)
return null
}
......@@ -1933,7 +1940,6 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
const { stream: prefetchStream, size: responseSize } =
await createNonTaskyPrefetchResponseStream(response.body)
closed.resolve()
setSizeInCacheMap(entry, responseSize)
const serverData =
await createFromNextReadableStream<NavigationFlightResponse>(
......@@ -1942,11 +1948,9 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
{ allowPartialStream: true }
)
if (
(response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b) !==
getNavigationBuildId()
) {
rejectRouteCacheEntry(entry, now + 10 * 1000)
const buildId =
response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b
if (buildId !== getNavigationBuildId()) {
return null
}
......@@ -1964,10 +1968,52 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
const flightDatas = normalizeFlightData(patchedFlightData)
if (typeof flightDatas === 'string') {
return null
}
return {
buildId,
closed: closed.promise,
couldBeIntercepted: serverData.i,
flightDatas,
headVaryParams,
responseSize,
staleAt: await getStaleAt(now, serverData.s),
supportsPerSegmentPrefetching: serverData.S,
}
}
async function fetchRouteOnCacheMissFromOutputExportFallback(
entry: PendingRouteCacheEntry,
key: RouteCacheKey,
headers: RequestHeaders
): Promise<PrefetchSubtaskResult<null> | null> {
const now = Date.now()
const renderedUrl = new URL(key.pathname + key.search, location.origin)
const navigationData = await fetchOutputExportFallbackNavigationData(
renderedUrl,
headers,
now
)
if (navigationData === null) {
rejectRouteCacheEntry(entry, now + 10 * 1000)
return null
}
const {
buildId,
closed,
couldBeIntercepted,
flightDatas,
headVaryParams,
responseSize,
staleAt,
supportsPerSegmentPrefetching,
} = navigationData
const renderedSearch = renderedUrl.search as NormalizedSearch
setSizeInCacheMap(entry, responseSize)
const navigationSeed = convertServerPatchToFullTree(
now,
DynamicRequestTreeForEntireRoute,
......@@ -1980,15 +2026,14 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
return null
}
const fetchStrategy = serverData.S
const fetchStrategy = supportsPerSegmentPrefetching
? FetchStrategy.PPR
: FetchStrategy.LoadingBoundary
const staleAt = await getStaleAt(now, serverData.s)
writeDynamicRenderResponseIntoCache(
now,
fetchStrategy,
flightDatas,
response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b,
buildId,
false,
headVaryParams,
staleAt,
......@@ -1996,7 +2041,6 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
null
)
const couldBeIntercepted = serverData.i
const fulfilledEntry = discoverKnownRoute(
now,
renderedUrl.pathname,
......@@ -2006,7 +2050,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
navigationSeed.metadataVaryPath,
couldBeIntercepted,
createHrefFromUrl(renderedUrl),
serverData.S,
supportsPerSegmentPrefetching,
false
)
fulfilledEntry.hasInlinedSegments = true
......@@ -2022,7 +2066,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
setInCacheMap(routeCacheMap, fulfilledVaryPath, entry, isRevalidation)
}
return { value: null, closed: closed.promise }
return { value: null, closed }
}
async function fetchSegmentsFromOutputExportFallback(
......@@ -2036,60 +2080,20 @@ async function fetchSegmentsFromOutputExportFallback(
routeKey.pathname + routeKey.search,
location.origin
)
const fallbackResult = await fetchOutputExportFallbackResponse(renderedUrl, {
credentials: 'same-origin',
const navigationData = await fetchOutputExportFallbackNavigationData(
renderedUrl,
headers,
})
if (fallbackResult === null) {
rejectRemainingSegmentsInBundle(segments, now + 10 * 1000)
return null
}
const { response } = await processFetch(fallbackResult.response)
if (!response.body) {
rejectRemainingSegmentsInBundle(segments, now + 10 * 1000)
return null
}
const closed = createPromiseWithResolvers<void>()
const { stream: prefetchStream } = await createNonTaskyPrefetchResponseStream(
response.body
now
)
closed.resolve()
const serverData =
await createFromNextReadableStream<NavigationFlightResponse>(
prefetchStream,
headers,
{ allowPartialStream: true }
)
if (
(response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b) !==
getNavigationBuildId()
) {
if (navigationData === null) {
rejectRemainingSegmentsInBundle(segments, now + 10 * 1000)
return null
}
const { buildId, closed, flightDatas, headVaryParams, staleAt } =
navigationData
const renderedSearch = renderedUrl.search as NormalizedSearch
const headVaryParamsThenable = serverData.h
const headVaryParams =
headVaryParamsThenable !== null
? readVaryParams(headVaryParamsThenable)
: null
const patchedFlightData = fillInFallbackFlightData(
serverData.f,
renderedUrl.pathname,
renderedSearch
)
const flightDatas = normalizeFlightData(patchedFlightData)
if (typeof flightDatas === 'string') {
rejectRemainingSegmentsInBundle(segments, now + 10 * 1000)
return null
}
const spawnedEntries = new Map<SegmentRequestKey, PendingSegmentCacheEntry>()
let node: SegmentBundle | null = segments
......@@ -2122,12 +2126,11 @@ async function fetchSegmentsFromOutputExportFallback(
// FetchStrategy.PPR; upsertSegmentEntry rejects candidates whose strategy
// is "less specific" than the existing entry. Using LoadingBoundary here
// would cause the fallback-written segments to be silently rejected.
const staleAt = await getStaleAt(now, serverData.s)
writeDynamicRenderResponseIntoCache(
now,
FetchStrategy.PPR,
flightDatas,
response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b,
buildId,
false,
headVaryParams,
staleAt,
......@@ -2135,7 +2138,7 @@ async function fetchSegmentsFromOutputExportFallback(
spawnedEntries
)
return { value: null, closed: closed.promise }
return { value: null, closed }
}
function rejectRemainingSegmentsInBundle(
......
......@@ -246,7 +246,7 @@ describe('output export fallback helpers', () => {
)
})
it('caches fallback metadata lookups across sibling routes', async () => {
it('dedupes fallback artifact fetches across sibling routes', async () => {
const fetchMock = jest.fn(async (input: RequestInfo | URL) => {
const url = String(input)
......@@ -297,7 +297,6 @@ describe('output export fallback helpers', () => {
expect(fetchMock.mock.calls.map(([url]) => String(url))).toEqual([
'https://example.com/docs/__fallback.meta.json',
'https://example.com/docs/__fallback/__route_0.txt',
'https://example.com/docs/__fallback/__route_0.txt',
])
})
})
......@@ -12,37 +12,40 @@ type OutputExportFallbackManifest = {
routes: OutputExportFallbackManifestEntry[]
}
const outputExportFallbackManifestCache = new Map<
string,
Promise<OutputExportFallbackManifest | null>
>()
function getOutputExportFallbackDataUrlCache(): Map<string, string> {
const globalWithCache = globalThis as typeof globalThis & {
__NEXT_OUTPUT_EXPORT_FALLBACK_DATA_URL_CACHE?: Map<string, string>
}
const existing = globalWithCache.__NEXT_OUTPUT_EXPORT_FALLBACK_DATA_URL_CACHE
if (existing !== undefined) {
return existing
}
type OutputExportFallbackCacheStore = {
manifests: Map<string, Promise<OutputExportFallbackManifest | null>>
dataResponses: Map<string, Promise<OutputExportCachedResponse | null>>
dataUrls: Map<string, string>
basePaths: Map<string, string>
}
const cache = new Map<string, string>()
globalWithCache.__NEXT_OUTPUT_EXPORT_FALLBACK_DATA_URL_CACHE = cache
return cache
type OutputExportCachedResponse = {
body: ArrayBuffer
headers: Array<[string, string]>
status: number
statusText: string
}
function getOutputExportFallbackBasePathCache(): Map<string, string> {
function getOutputExportFallbackCacheStore(): OutputExportFallbackCacheStore {
const globalWithCache = globalThis as typeof globalThis & {
__NEXT_OUTPUT_EXPORT_FALLBACK_BASE_PATH_CACHE?: Map<string, string>
__NEXT_OUTPUT_EXPORT_FALLBACK_CACHE_STORE?: OutputExportFallbackCacheStore
}
const existing = globalWithCache.__NEXT_OUTPUT_EXPORT_FALLBACK_BASE_PATH_CACHE
const existing = globalWithCache.__NEXT_OUTPUT_EXPORT_FALLBACK_CACHE_STORE
if (existing !== undefined) {
return existing
}
const cache = new Map<string, string>()
globalWithCache.__NEXT_OUTPUT_EXPORT_FALLBACK_BASE_PATH_CACHE = cache
return cache
const cacheStore = {
manifests: new Map<string, Promise<OutputExportFallbackManifest | null>>(),
dataResponses: new Map<
string,
Promise<OutputExportCachedResponse | null>
>(),
dataUrls: new Map<string, string>(),
basePaths: new Map<string, string>(),
}
globalWithCache.__NEXT_OUTPUT_EXPORT_FALLBACK_CACHE_STORE = cacheStore
return cacheStore
}
export function getOutputExportFallbackCandidates(pathname: string): string[] {
......@@ -78,9 +81,11 @@ export function isOutputExportFlightContentType(contentType: string): boolean {
}
export function clearOutputExportFallbackManifestCache(): void {
outputExportFallbackManifestCache.clear()
getOutputExportFallbackDataUrlCache().clear()
getOutputExportFallbackBasePathCache().clear()
const cacheStore = getOutputExportFallbackCacheStore()
cacheStore.manifests.clear()
cacheStore.dataResponses.clear()
cacheStore.dataUrls.clear()
cacheStore.basePaths.clear()
}
export function stripOutputExportDataSuffix(url: URL): URL {
......@@ -113,7 +118,8 @@ async function fetchOutputExportFallbackManifest(
): Promise<OutputExportFallbackManifest | null> {
const metadataUrl = getOutputExportFallbackMetadataUrl(fallbackUrl)
const cacheKey = metadataUrl.href
const cached = outputExportFallbackManifestCache.get(cacheKey)
const manifestCache = getOutputExportFallbackCacheStore().manifests
const cached = manifestCache.get(cacheKey)
if (cached !== undefined) {
return cached
}
......@@ -135,11 +141,11 @@ async function fetchOutputExportFallbackManifest(
return metadataResponse.json()
})
.catch((error) => {
outputExportFallbackManifestCache.delete(cacheKey)
manifestCache.delete(cacheKey)
throw error
})
outputExportFallbackManifestCache.set(cacheKey, manifestPromise)
manifestCache.set(cacheKey, manifestPromise)
return manifestPromise
}
......@@ -169,18 +175,18 @@ function cacheOutputExportFallbackDataUrl(
fallbackUrl: URL,
resolvedDataUrl: URL
) {
const dataUrlCache = getOutputExportFallbackDataUrlCache()
const { dataUrls, basePaths } = getOutputExportFallbackCacheStore()
for (const candidateUrl of getOutputExportDataCandidates(renderedUrl)) {
dataUrlCache.set(candidateUrl.href, resolvedDataUrl.href)
dataUrls.set(candidateUrl.href, resolvedDataUrl.href)
}
getOutputExportFallbackBasePathCache().set(
basePaths.set(
normalizeOutputExportRouteDirectory(renderedUrl.pathname),
normalizeOutputExportRouteDirectory(fallbackUrl.pathname)
)
}
export function getCachedOutputExportFallbackDataUrl(url: URL): URL | null {
const cached = getOutputExportFallbackDataUrlCache().get(url.href)
const cached = getOutputExportFallbackCacheStore().dataUrls.get(url.href)
return cached !== undefined ? new URL(cached) : null
}
......@@ -199,7 +205,7 @@ export function getCachedOutputExportFallbackRequestUrl(url: URL): URL | null {
url.pathname.slice(0, url.pathname.lastIndexOf('/')) || '/'
)
const fallbackBasePath =
getOutputExportFallbackBasePathCache().get(routeDirectory)
getOutputExportFallbackCacheStore().basePaths.get(routeDirectory)
if (fallbackBasePath === undefined) {
return null
}
......@@ -214,13 +220,8 @@ async function fetchOutputExportDataResult(
init?: RequestInit
): Promise<{ response: Response; dataUrl: URL } | null> {
for (const dataUrl of getOutputExportDataCandidates(renderedUrl)) {
const response = await fetch(dataUrl, init)
const contentType = response.headers.get('content-type') || ''
if (
response.ok &&
response.body &&
isOutputExportFlightContentType(contentType)
) {
const response = await fetchOutputExportDataResponseByUrl(dataUrl, init)
if (response !== null) {
return { response, dataUrl }
}
}
......@@ -228,6 +229,52 @@ async function fetchOutputExportDataResult(
return null
}
async function fetchOutputExportDataResponseByUrl(
dataUrl: URL,
init?: RequestInit
): Promise<Response | null> {
const cacheKey = dataUrl.href
const dataResponseCache = getOutputExportFallbackCacheStore().dataResponses
let cachedResponse = dataResponseCache.get(cacheKey)
if (cachedResponse === undefined) {
cachedResponse = fetch(dataUrl, init)
.then(async (response) => {
const contentType = response.headers.get('content-type') || ''
if (
!response.ok ||
!response.body ||
!isOutputExportFlightContentType(contentType)
) {
return null
}
return {
body: await response.arrayBuffer(),
headers: Array.from(response.headers.entries()),
status: response.status,
statusText: response.statusText,
}
})
.catch((error) => {
dataResponseCache.delete(cacheKey)
throw error
})
dataResponseCache.set(cacheKey, cachedResponse)
}
const response = await cachedResponse
if (response === null) {
return null
}
return new Response(response.body.slice(0), {
headers: response.headers,
status: response.status,
statusText: response.statusText,
})
}
export async function fetchOutputExportDataResponse(
renderedUrl: URL,
init?: RequestInit
......
......@@ -10,6 +10,11 @@ export default function Isolated() {
prefetched isolated third page
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/isolated/fourth" prefetch={true}>
prefetched isolated fourth page
</LinkAccordion>
</li>
</ul>
</main>
)
......
......@@ -173,6 +173,40 @@ if (skipped) {
}
})
it('dedupes fallback artifact prefetches across sibling unknown-param links', async () => {
let act!: ReturnType<typeof createRouterAct>
const browser = await webdriver(port, '/isolated/', {
beforePageLoad(page: Playwright.Page) {
act = createRouterAct(page)
},
})
try {
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('Isolated')
})
clearRequests()
await act(async () => {
await browser
.elementByCss('input[data-link-accordion="/isolated/third"]')
.click()
await browser
.elementByCss('input[data-link-accordion="/isolated/fourth"]')
.click()
})
const prefetchRequests = getRequests().filter((requestPath) =>
requestPath.startsWith('/isolated/__fallback/index.txt')
)
expect(prefetchRequests).toHaveLength(1)
} finally {
await browser.close()
}
})
it('renders the app not-found page when no fallback route matches', async () => {
const browser = await webdriver(port, '/missing/route/')
......
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