The Caching Layers
A version-qualified path through the Next.js App Router: server components, client boundaries, data fetching, caching layers, streaming, error handling, proxy, images, performance, testing and deployment, with the Next.js 15 and 16 behavior changes called out where they matter.
What you will be able to do
#- Name the caching layers and what each one caches in the pre-Cache-Components model.
- Invalidate precisely with revalidateTag, revalidatePath and time-based revalidation.
- State the version-qualified changes to defaults and signatures through Next.js 16.
Overview
#Four layers in the previous model
Without Cache Components, four caches cooperate. Request memoization dedupes identical fetches within one server render (React-level, per request). The Data Cache persists fetch results across requests, keyed by URL and options. The Full Route Cache persists rendered route output at build or revalidation time. The Client Router Cache keeps visited route payloads in the browser for back/forward and prefetch. Knowing which layer a bug lives in is the debugging skill: wrong data for everyone points at the Data Cache or Full Route Cache; wrong data only after navigation points at the Router Cache; duplicate identical fetches in one render point at missing memoization.
Time-based revalidation
fetch(url, { next: { revalidate: 60 } }) marks the entry stale after 60 seconds; the next request serves the cached value and refreshes in the background (stale-while-revalidate). A route segment can set export const revalidate = 60 as a default for its fetches, and export const dynamic = 'force-dynamic' opts the whole segment out of prerendering.
On-demand invalidation
Tag fetches with next: { tags: ['products'] } or wrap non-fetch reads in unstable_cache(fetcher, keys, { tags, revalidate }), then invalidate from a Server Action or Route Handler. Version-qualified signature: in Next.js 16, revalidateTag requires a second argument, a cacheLife profile such as revalidateTag('products', 'max'), which serves stale content while revalidating; the old single-argument form is deprecated and behaves like { expire: 0 } (next request blocks for fresh data). revalidatePath('/products') discards cached output for a path and is unchanged. For read-your-own-writes after a mutation, Next.js 16 adds updateTag('products'), usable only in Server Actions, which expires the tag so the user's next request waits for fresh data.
Router cache nuances
The client Router Cache caches segment payloads per navigation session. A router.refresh() re-fetches the current route from the server; a full page reload clears the cache. Stale-after-navigation reports are usually the Router Cache serving a prefetched payload; the fix is correct invalidation server-side so the next prefetch gets fresh data.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Next.js 16 code. A fetch is tagged 'products'. A Server Action calls revalidateTag('products') with no second argument, then the user navigates to the products list. What happens, and what is the corrected call?
Exercise 2Explain
A bug report says: after an admin edits a product, some users see the old price and some see the new one, and the same user can see both after navigating. Diagnose which cache layers are involved and give the fix.
Reveal the reference answer
reference.textUsers seeing different values points at cached server output or fetch results that were invalidated for some entry paths but not others. The same user seeing both values after navigating points at the client Router Cache holding a prefetched stale payload in one tab history entry while a fresh one loads in another. Fix: tag the product reads and invalidate the tag in the edit Server Action (revalidateTag('products', 'max') or updateTag for read-your-own-writes), and revalidatePath the product list route if its rendered output is cached. If the stale value persists only in an open tab, a router.refresh() or the next prefetch after invalidation resolves it.Authored expected output: Data Cache or Full Route Cache for cross-user staleness, Router Cache for per-session staleness, tag-based invalidation as the fix.
Layer identification turns a vague staleness report into a one-line fix.
Exercise 3Implement
Cache a database read (not fetch) for 60 seconds with tags in the previous caching model, then invalidate it from a Server Action in Next.js 16 form.
starter.ts// lib/data.ts // TODO: wrap getProducts in unstable_cache with tag 'products' and revalidate 60 // app/actions.ts // TODO: Server Action that adds a product and invalidatesReveal the reference answer
reference.ts// lib/data.ts import { unstable_cache } from 'next/cache'; export const getProducts = unstable_cache( async () => db.product.findMany(), ['products-list'], { tags: ['products'], revalidate: 60 }, ); // app/actions.ts 'use server'; import { revalidateTag } from 'next/cache'; export async function addProduct(data: NewProduct) { await db.product.create({ data }); revalidateTag('products', 'max'); }Authored expected output: The list read is cached across requests and refreshed after the mutation with stale-while-revalidate semantics.
unstable_cache is the non-fetch counterpart of the Data Cache; the 16 profile argument appears in the invalidation call.
Exercise 4Debug
This page shows the same random-ish 'featured products' for every user for up to an hour, but the requirement is per-request freshness. Find the cause and fix it. export const revalidate = 3600; export default async function Page() { const featured = await fetch('https://api.example.com/featured').then((r) => r.json()); return <Gallery items={featured} />; }
starter.tsxexport const revalidate = 3600; export default async function Page() { const featured = await fetch('https://api.example.com/featured').then((r) => r.json()); return <Gallery items={featured} />; }Reveal the reference answer
reference.tsxThe segment-level revalidate = 3600 makes the whole route cached for an hour, and the fetch inherits that default. For per-request freshness remove the segment revalidate and fetch with cache: 'no-store' (or no options, since Next.js 15 defaults to uncached). If the page must stay fast, keep the shell static and move the featured section into a Suspense-wrapped component that reads uncached data at request time.Authored expected output: Featured products differ per request while the rest of the page stays fast.
Segment config is a route-wide default; per-call options and boundaries carve out exceptions.
Exercise 5Test
Write a pure function isFresh(entry: { storedAt: number; revalidate: number }, now: number) implementing stale-while-revalidate freshness: returns 'fresh' when now - storedAt < revalidate, 'stale-but-serving' when it is within 5x revalidate, 'expired' beyond that. Assert with revalidate 60: at 30 seconds fresh, at 90 stale-but-serving, at 400 expired.
starter.tsexport function isFresh( entry: { storedAt: number; revalidate: number }, now: number, ): 'fresh' | 'stale-but-serving' | 'expired' { // TODO }Reveal the reference answer
reference.tsexport function isFresh( entry: { storedAt: number; revalidate: number }, now: number, ): 'fresh' | 'stale-but-serving' | 'expired' { const age = now - entry.storedAt; if (age < entry.revalidate) return 'fresh'; if (age < entry.revalidate * 5) return 'stale-but-serving'; return 'expired'; } // The 5x window models the stale-while-revalidate serving period conceptually.Authored expected output: All three assertions pass.
Distinguishing fresh, stale-but-served, and expired is the core of revalidation reasoning.
Exercise 6Interview
An interviewer asks: explain Next.js caching to a new teammate in five minutes. Which layers do you cover, and how do you decide where a given piece of data should be cached?
Reveal the reference answer
reference.textCover four layers with one sentence each: request memoization (dedupe within one render), Data Cache (fetch results across requests), Full Route Cache (rendered route output), Router Cache (client-side navigation payloads). Then give the decision procedure: ask who may see the data and how stale it may be. Same for everyone and minutes-stale: Data Cache with revalidate or tags. Same for everyone and stale only on publish: cache with tags and invalidate on the write path. Per-user: never Data Cache; render at request time or cache keyed by user only if the platform supports it. Navigation feel: Router Cache is automatic, so the lever is server invalidation correctness. Close with the version note: since Next.js 15 fetch is uncached by default, and Next.js 16 adds Cache Components with use cache as the directive-based model.Authored expected output: Four layers plus a visibility-and-staleness decision procedure plus version notes.
The decision procedure is what separates recitation from understanding.
Common misconceptions
#- revalidateTag takes only a tag. Since Next.js 16 it requires a profile argument such as 'max'; the single-argument form is deprecated.
- revalidate = 60 blocks requests for fresh data. It serves the cached value and refreshes in the background; blocking behavior is expire semantics.
- The Router Cache is the Data Cache. One is client-side navigation state; the other is server-side fetch results.
- unstable_cache caches fetch calls. It is for non-fetch work like database queries; fetch has its own options.
Follow-up practice
#- nextjs-cache-components
- nextjs-server-actions
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.