fix(incremental-cache): keep an ISR entry's cache lifetime across instances and restarts (#99289)
### What? An on-demand ISR page (dynamic route with `revalidate`, not prerendered at build) loses its `Cache-Control` header when the process serving it from the cache is not the one that rendered it. That happens with several instances sharing a cache handler, and with a single instance after a restart, including the default file-system cache. After a restart it is also served `STALE` on every request, because `calculateRevalidate` falls back to its 1-second default. A running instance can also retain an outdated lifetime after another instance replaces the shared entry with a shorter-lived result. ### Why? `IncrementalCache.get()` takes an entry's lifetime only from `SharedCacheControls`: an in-memory map that `IncrementalCache.set()` fills in the same process, with the prerender manifest as fallback. An on-demand page of a dynamic route is not in the manifest. So any other process has no lifetime for the entry, sends no header, and treats the entry as stale after one second. ### How? - `CacheHandlerValue` gets an optional `cacheControl`. Handlers already receive it as `ctx.cacheControl` in `set()`; now they can return it from `get()`. - In `IncrementalCache.get()`, the lifetime returned with the entry takes precedence over the in-memory map and is remembered for the route, so both the response header and revalidation use the entry's current lifetime. - The default `FileSystemCache` stores `cacheControl` in the `.meta` file of App Router pages and routes, and returns it. Pages Router ISR is not covered: the file-system cache writes no `.meta` for it. I kept that out of this PR. ### Tests `test/production/app-dir/isr-cache-control-restart` contains three separate on-disk fixture apps: - `default`: the built-in file-system cache, with `cacheComponents: false`. - `custom`: a persistent custom cache handler that stores `ctx.cacheControl`, with `cacheComponents: false`. - `cache-components`: `cacheLife` with a persistent custom route-cache handler, covering the same on-demand route-cache path with Cache Components enabled. The three restart cases render an on-demand page, age the entry beyond two seconds, and restart the server without rebuilding. The first response after restart must retain the original `Cache-Control` and report `HIT`; a repeat request must also report `HIT`. Aging past the broken one-second fallback makes the freshness assertion meaningful. A fourth test keeps servers A and B running from the same build with one shared cache handler and a controllable service. A first caches a success with a one-hour lifetime. After direct eviction from the shared store, B caches an error result with a two-second lifetime. A must use the replacement entry's shorter `Cache-Control` and report `STALE` after two seconds. Direct eviction avoids relying on the fixture handler's no-op tag invalidation. All four tests pass locally with Webpack and with Turbopack under `__NEXT_CACHE_COMPONENTS=true`. The new two-server test catches the previous implementation retaining A's one-hour lifetime. The three restart cases were also verified to fail on the original PR base (`1cc0f661`) with `STALE` instead of `HIT`. Prettier, ESLint, the root and Next.js type checks, and the full repository build pass. The local validation above ran on `7adcd57b`, before the subsequent merge of `canary` (`d093eb45`). That merge preserved the changed incremental-cache files and fixtures. Fixes #99288 --------- Co-authored-by:orzazade <orzazade@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment