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

PartialPrefetching Causes Four Renders Per On-Demand Page, Timer-Retained Render State Leads To Large Memory Spikes

With partialPrefetching enabled, dynamic pages are rendered four times per request (request, runtime prefetch, shell upgrade x2). Timers scheduled during each render capture AsyncLocalStorage context and retain large React Flight request and cache state, causing memory retention of ~3.8 MiB per request for a 10-minute timer, up to 45 GiB at 20 rps.

highConfidence 92%Next.jsAffected V16.3.5Affected V16.3.6Affected V16.4.0-Canary.39

Origin Analysis

Next.js spawns two additional background render phases when partialPrefetching is active: spawnRuntimePrefetchWithFilledCaches and the fallback shell upgrade via responseCache.revalidate. Each phase executes user code and schedules timers; those timers inherit the render's AsyncLocalStorage store, which holds workUnitStore, React Flight Request, cacheController and abort-reason Error frames. The references are not cleared after background renders settle, so timers pin the entire render state until they fire.
1. Clone https://github.com/giovannetti-eric/next-partial-prefetching-memory-repro and npm install. 2. Build/start with PARTIAL_PREFETCHING=true and NODE_OPTIONS=--expose-gc. 3. Run count-timers.mjs to observe 4 renders per request. 4. Run load.mjs to measure retained heap after forced GC; compare with PARTIAL_PREFETCHING=false. 5. Observe 3.8 MiB retained per request for 600s timers vs 0.55 MiB without flag.

Fixing Code Block

Edge Case Audit

Disabling partialPrefetching loses the prefetch performance benefit and may not be acceptable for apps that rely on partial prefetching. Setting gcTime=Infinity on server may change cache eviction behavior for client-side hydration? Actually it only affects server, so safe. The config change is easily reversible; if you revert, the memory issue returns. Upgrading Next.js may not fix until a patch is released. Test thoroughly for regressions in caching and PPR behavior.

Ecosystem Topology