advanced

Caching

Reason about request memoization, data cache, full-route cache, router cache, CDN, and cache invalidation.

Next.js stacks several caches: request memoization during render, Data Cache for `fetch`, Full Route Cache for static HTML/RSC, and client Router Cache for navigations. CDN and browser caches sit outside the framework.

| Layer | Scope | Typical invalidation | |-------|-------|----------------------| | Request memo | Single render | New request | | Data Cache | fetch results | revalidate/time/tags | | Full Route Cache | Static routes | revalidatePath/Tag | | Router Cache | Client navigations | Hard refresh, mutations |

On interviews: name which layer serves a repeat visitor and what makes data stale.

Common pitfalls: assuming `fetch` is always uncached, disabling all caches as a default, and personalized data in shared cache keys.

The trade-off is performance versus predictable freshness — document both.

Checklist:

  • Know default cache per fetch option.
  • Tag related reads for grouped invalidation.
  • Keep auth-scoped data dynamic or private.
  • Test CDN headers separately from Data Cache.