Commit a1779059 authored by Jimmy Lai's avatar Jimmy Lai

Dedupe output export fallback client fetches

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