Unverified Commit 4b4b878c authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Revert "Record fallback cache lifetime for blocking PPR routes (#97821)" (#97835)

Reverts #97821, which shipped in v16.4.0-canary.5 and broke the deploy
e2e tests (run 32770221068): segment-cache/prefetch-app-shell's "uses
separate static App Shells for links with different root params" fails
deterministically.

The reverted change stamped the collected cache lifetime onto the cache
entries seeded from fallback files. Those entries are placeholders that
exist to be replaced — their short default lifetime is what drives the
regeneration that specializes them — so a blocking route's segment
entries were served as static-forever placeholders and the App Shell for
a non-prerendered root param never arrived.

A redesigned fix that records the rendered-content lifetime in separate,
additive fields (leaving the seeded-entry lifetime untouched) is staged
on `acdlite/fallback-entry-lifetimes`.
parent 2cee98ab
...@@ -1261,14 +1261,6 @@ export async function handleBuildComplete({ ...@@ -1261,14 +1261,6 @@ export async function handleBuildComplete({
ctx: { ctx: {
htmlAllowQuery?: string[] htmlAllowQuery?: string[]
dataAllowQuery?: string[] dataAllowQuery?: string[]
// The cache lifetime of the route's fallback artifacts, from the
// prerender manifest. Segment outputs attach this when the route
// has no HTML fallback output to inherit it from (a blocking
// route): the segment payloads are cached and served even then,
// and platforms interpret a missing revalidate as a 1-second
// lifetime, so segment outputs must always carry one.
fallbackRevalidate?: Revalidate
fallbackExpire?: number
} }
) => { ) => {
if (meta.postponed && initialOutput.fallback) { if (meta.postponed && initialOutput.fallback) {
...@@ -1334,12 +1326,8 @@ export async function handleBuildComplete({ ...@@ -1334,12 +1326,8 @@ export async function handleBuildComplete({
fallback: { fallback: {
filePath: fallbackPathname, filePath: fallbackPathname,
postponedState: undefined, postponedState: undefined,
initialExpiration: initialExpiration: initialOutput.fallback?.initialExpiration,
initialOutput.fallback?.initialExpiration ?? initialRevalidate: initialOutput.fallback?.initialRevalidate,
ctx.fallbackExpire,
initialRevalidate:
initialOutput.fallback?.initialRevalidate ??
ctx.fallbackRevalidate,
initialHeaders: { initialHeaders: {
...meta.headers, ...meta.headers,
...@@ -1939,8 +1927,6 @@ export async function handleBuildComplete({ ...@@ -1939,8 +1927,6 @@ export async function handleBuildComplete({
await handleAppMeta(dynamicRoute, initialOutput, meta, { await handleAppMeta(dynamicRoute, initialOutput, meta, {
htmlAllowQuery, htmlAllowQuery,
dataAllowQuery, dataAllowQuery,
fallbackRevalidate,
fallbackExpire,
}) })
} }
...@@ -1954,15 +1940,6 @@ export async function handleBuildComplete({ ...@@ -1954,15 +1940,6 @@ export async function handleBuildComplete({
filePath: undefined, filePath: undefined,
postponedState: meta.postponed, postponedState: meta.postponed,
initialStatus: undefined, initialStatus: undefined,
// A blocking route has no HTML fallback to spread these
// from, but its on-demand RSC template renders are still
// cached, so give those entries the lifetime recorded in
// the prerender manifest.
initialExpiration:
initialOutput.fallback?.initialExpiration ?? fallbackExpire,
initialRevalidate:
initialOutput.fallback?.initialRevalidate ??
fallbackRevalidate,
initialHeaders: { initialHeaders: {
...initialOutput.fallback?.initialHeaders, ...initialOutput.fallback?.initialHeaders,
...dataInitialHeaders, ...dataInitialHeaders,
......
...@@ -3828,33 +3828,20 @@ export default async function build( ...@@ -3828,33 +3828,20 @@ export default async function build(
routeResult?.hasEmptyStaticShell routeResult?.hasEmptyStaticShell
) )
// The cache lifetime collected while prerendering this route's // When the route is configured to serve a prerender, we should
// fallback. It's recorded for every PPR route, not just those // use the cache control from the export result. If it can't be
// that serve an HTML fallback: the fallback prerender runs // found, mark that we should keep the shell forever
// either way, and the artifacts it produces — the per-segment // (revalidate: `false` via `getCacheControl()`).
// prefetch payloads and the RSC template — are cached and const fallbackCacheControl =
// served even when the HTML fallback itself is not isRoutePPREnabled && fallbackMode === FallbackMode.PRERENDER
// (`fallback: null`, blocking). Cache layers use this value to ? cacheControl
// decide when entries for URLs without their own prerender go : undefined
// stale, and they interpret a missing value as a 1-second
// lifetime, so it must be present whenever such entries can
// exist. When the export collected no cache control,
// `getCacheControl()` returns revalidate: `false` (keep until
// invalidated).
const fallbackCacheControl = isRoutePPREnabled
? cacheControl
: undefined
const fallback: Fallback = fallbackModeToFallbackField( const fallback: Fallback = fallbackModeToFallbackField(
fallbackMode, fallbackMode,
route.pathname route.pathname
) )
// Unlike the cache lifetime above, the status and headers
// describe the prerendered fallback response itself, so they
// only apply when a fallback is actually served (PRERENDER
// mode). A blocking route's on-demand renders produce their
// own status and headers per request.
const meta = const meta =
metadata && metadata &&
isRoutePPREnabled && isRoutePPREnabled &&
......
...@@ -490,65 +490,4 @@ describe('adapter-prerender-metadata', () => { ...@@ -490,65 +490,4 @@ describe('adapter-prerender-metadata', () => {
expectUnclassified(segment) expectUnclassified(segment)
} }
}) })
it('records the fallback lifetime for blocking dynamic routes', async () => {
// A route whose cached shell is keyed on its params serves no HTML
// fallback (`fallback: null`), but its per-segment prefetch payloads and
// RSC template are still served and cached. The manifest must carry the
// shell's cache lifetime so consumers don't fall back to their
// 1-second default.
const manifest = await getPrerenderManifest()
expect(manifest.dynamicRoutes['/blocking-cached/[id]']).toMatchObject({
fallback: null,
fallbackRevalidate: 3600,
fallbackExpire: 86400,
})
})
it('gives segment outputs of a blocking route the shell cache lifetime', async () => {
const prerenders = await getPrerenders()
const segments = prerenders.filter((output) =>
output.pathname.startsWith('/blocking-cached/[id].segments/')
)
expect(segments.length).toBeGreaterThan(0)
for (const segment of segments) {
expect(segment.fallback.initialRevalidate).toBe(3600)
expect(segment.fallback.initialExpiration).toBe(86400)
}
const templateRsc = prerenders.find(
(output) => output.pathname === '/blocking-cached/[id].rsc'
)
expect(templateRsc).toBeDefined()
expect(templateRsc.fallback.initialRevalidate).toBe(3600)
expect(templateRsc.fallback.initialExpiration).toBe(86400)
// Concrete generated paths carry the same lifetime through the
// routes manifest.
const concreteSegments = prerenders.filter((output) =>
output.pathname.startsWith('/blocking-cached/known.segments/')
)
expect(concreteSegments.length).toBeGreaterThan(0)
for (const segment of concreteSegments) {
expect(segment.fallback.initialRevalidate).toBe(3600)
expect(segment.fallback.initialExpiration).toBe(86400)
}
})
it('never emits a segment output without a revalidate lifetime', async () => {
// Regression test: segment outputs used to inherit the revalidate of the
// route's HTML fallback, which doesn't exist for blocking routes. The
// resulting `undefined` was interpreted downstream as a 1-second TTL,
// causing an ISR cache write on nearly every prefetch of a
// non-prerendered URL.
const prerenders = await getPrerenders()
const segments = prerenders.filter((output) =>
output.pathname.includes('.segments/')
)
expect(segments.length).toBeGreaterThan(0)
for (const segment of segments) {
expect(segment.fallback.initialRevalidate).toBeDefined()
}
})
}) })
import { Suspense } from 'react'
import { cookies } from 'next/headers'
import { cacheLife } from 'next/cache'
// A dynamic route whose cached shell is keyed on its params. The fallback
// shell prerender suspends at the shell, so no HTML fallback is emitted
// (`fallback: null`, blocking) — but per-segment prefetch payloads are still
// written and served, and their lifetime must reflect the cache lifetimes
// collected during the fallback prerender (the layout's CachedBanner).
async function Shell({
params,
children,
}: {
params: Promise<{ id: string }>
children: React.ReactNode
}) {
'use cache'
// Same lifetime as the layout's CachedBanner, so the concrete generated
// path (which collects both caches) agrees with the fallback export
// (which only collects the banner).
cacheLife({ stale: 300, revalidate: 3600, expire: 86400 })
const { id } = await params
return (
<div>
<h1>Shell for {id}</h1>
{children}
</div>
)
}
async function Dynamic() {
const jar = await cookies()
return <p>session: {jar.get('session')?.value ?? 'anon'}</p>
}
export async function generateStaticParams() {
return [{ id: 'known' }]
}
export default async function Page({
params,
}: {
params: Promise<{ id: string }>
}) {
return (
<Shell params={params}>
<Suspense fallback={<p>loading…</p>}>
<Dynamic />
</Suspense>
</Shell>
)
}
import { ReactNode } from 'react'
import { cacheLife } from 'next/cache'
// A cached component that completes during the fallback shell prerender (it
// doesn't read params), so its cacheLife propagates to the collected cache
// control of the fallback export.
async function CachedBanner() {
'use cache'
cacheLife({ stale: 300, revalidate: 3600, expire: 86400 })
return <p>banner</p>
}
export default function Layout({ children }: { children: ReactNode }) {
return (
<>
<CachedBanner />
{children}
</>
)
}
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