- Next.js
- Next.js Caching Architecture
Next.js Caching Architecture
Caching in Next.js operates across four distinct tiers: Request Memoization, Data Cache, Full Route Cache, and the in-memory client Router Cache.Understand the architectural shift from Next.js 14 aggressive fetch caching to modern explicit opt-in caching via Next.js 16 Cache Components and the "use cache" directive.Learn how to design surgical cache invalidation pipelines using revalidateTag and revalidatePath without triggering cache stampedes.
The Four Distinct Cache Layers in Next.js
Next.js incorporates four distinct caching layers: 1) Request Memoization (per-request in-memory deduplication via React.cache), 2) Data Cache (persistent cross-request data store on the server), 3) Full Route Cache (static HTML and RSC Flight payload cached on the server at build/revalidation time), and 4) Router Cache (client-side in-memory cache of RSC payloads during a user session).
Evolution of Caching: Next.js 14 Implicit vs Next.js 15+ Opt-In
In Next.js 14, fetch requests and Route Handlers were cached aggressively by default (force-cache), leading to widespread developer confusion regarding stale dynamic data. Next.js 15 reversed this paradigm: fetch requests now default to no-store (uncached), Route Handlers default to dynamic unless configured, and client Router Cache staleTime for dynamic routes was reduced to 0 seconds.
Modern Explicit Caching with "use cache" & Cache Components
In Next.js 16, Cache Components and the "use cache" directive introduce fine-grained, declarative caching at the component, function, or file level. Instead of configuring low-level HTTP fetch options, developers mark components or helper functions with "use cache", and configure retention using cacheLife() profiles and cacheTag() invalidation identifiers.
On-Demand Revalidation: revalidateTag vs revalidatePath
revalidateTag purges cache entries associated with a specific semantic cache key across the entire application, making it ideal for entity-based updates (e.g. updating a product). revalidatePath purges cached pages and route segments matching a URL path, making it ideal for page-level structural updates. Both invalidate the server Data Cache and Full Route Cache on-demand.
The Client-Side Router Cache & Stale Prefetch Pitfalls
The client-side Router Cache stores rendered React Server Component Flight payloads in browser memory during a user session. When users navigate, Next.js serves pages instantly from this client cache. However, if data changes on the server without client awareness, users see stale UI until the staleTime expires, router.refresh() is called, or a Server Action triggers automatic revalidation.
Dynamic Rendering vs Static Rendering Triggers
In Next.js App Router, routes are statically prerendered by default unless they consume dynamic functions (cookies(), headers(), searchParams) or an uncached data source. When a dynamic trigger is accessed, Next.js automatically switches that segment from static build-time generation to request-time dynamic rendering.
