AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

CacheComponents: Page Past Its Expire Returns 500 Due To Unstable_cache Skipping Resume Data Cache

When a static route's cache entry passes its `expire` time, Next.js sets `isOnDemandRevalidate = true` for the blocking regeneration. This causes `unstable_cache` to skip reading from the cache entirely, including the prerender resume data cache that was populated during the prospective prerender pass. As a result, the final pass fails with `NEXT_STATIC_GEN_BAILOUT` and returns HTTP 500.

highConfidence 85%Next.jsAffected V16.3.4Affected V16.3.8Affected V16.4.0Affected V16.5.0-Canary.3

Origin Analysis

In `app-page-runtime.js` (or `app-page.ts`), the condition `if (previousIncrementalCacheEntry?.isStale === -1) { isOnDemandRevalidate = true; }` incorrectly marks an expired entry as an on-demand revalidation. This flag causes `unstable_cache` to bypass all cache reads, including the resume data cache, leading to missing data during the blocking regeneration.
1. Clone the reproduction repo: `git clone https://github.com/tdejager/next-on-demand-unstable-cache-bailout-repro && cd next-on-demand-unstable-cache-bailout-repro && pnpm install` 2. Run `./repro.sh` which builds the app, then runs `next start` twice: control (fresh build) and aged (mtimes set to 30 minutes ago). 3. Observe that the aged run returns HTTP 500 on the first request with `NEXT_STATIC_GEN_BAILOUT` error, while control returns 200.

Fixing Code Block

Edge Case Audit

This change may affect other invalidation scenarios where `isStale === -1` is expected to trigger on-demand revalidation semantics. It should be thoroughly tested with CDN/custom cache handlers and concurrent requests. Rollback: revert the condition to its original form if any regressions appear, or apply the alternative patch of removing the `isOnDemandRevalidate` condition in `unstable_cache` (less targeted). Also note that this fix does not address potential similar issues in `patch-fetch` or nested `use cache` if they also check the same flag; further testing is required.

Ecosystem Topology