Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 72 ยท Data Fetching and Caching

Caching in Next.js: the Big Picture

Understand every caching layer in the Next.js App Router: request memoization, the data cache, the full route cache and the router cache, and what changed in 15 and 16.

Caching is the part of Next.js that surprises people most. A page shows old data when you wanted new data, or a page that should be fast hits the database on every visit. The reason is that Next.js caches in several places at once, each for a different purpose, and the rules were changed in versions 15 and 16. If you do not have a map, the behavior looks random.

This lesson is that map. You will learn the four classic layers, where each one lives, how long it lasts and what resets it. Then you will see what changed in the recent versions, where almost nothing is cached unless you ask, and a newer model called Cache Components. The lessons after this one go deep on each tool: the use cache directive, revalidation and static versus dynamic rendering.

Real Next.js cannot run on this site, so its code is read-only. The two runnable examples are small stand-ins: a Node.js model of the server layers and a React model of the browser's router cache.

Why there are layers

Think about what happens when a page is shown. The browser asks for a URL. The server renders a tree of components. Some of those components fetch data. Each step can be repeated work for the same answer, so each step can have its own cache.

Four layers, four jobs

Request memoization Same fetch twice in one render runs once.
Data cache Stores fetch results on the server across requests.
Full route cache Stores the finished HTML and payload of static pages.
Router cache Remembers visited pages in the browser.

Three live on the server, one in the browser.

1 / 2

Here is the same map as a flow. Follow one visit from the browser to the data source.

flowchart TD
  B["Browser asks for /blog"] --> RC{"Router cache<br/>has it?"}
  RC -- "yes" --> SHOW["Show it now"]
  RC -- "no" --> FR{"Full route cache<br/>(static page)?"}
  FR -- "hit" --> SEND["Send stored HTML and payload"]
  FR -- "miss" --> R["Render components"]
  R --> M{"Request memoization<br/>same fetch already made?"}
  M -- "yes" --> USE["Reuse the result"]
  M -- "no" --> DC{"Data cache<br/>has the response?"}
  DC -- "yes" --> USE
  DC -- "no" --> O["Call the data source"]
  O --> USE

A request meets the router cache in the browser first. On the server it meets the full route cache, memoization and the data cache before it reaches the data source.

The four layers

1. Request memoization

You met this in Fetching Data in Server Components. While one request is being rendered, identical GET fetches are shared, so a layout and a page can both ask for the current user and the network is used once. It lives in memory for the duration of the render and is never shared between users or requests. React provides it, and cache() extends the same idea to database calls.

2. The data cache

The data cache stores the result of a fetch on the server, and it persists across requests and, on most hosting setups, across deployments. It is how a page can show data without calling your API on every visit. You opt in with fetch options (this is the classic model, which still works when Cache Components are off):

TSX
// real Next.js, read-only
// Cache the response until it is revalidated:
await fetch('https://api.example.com/posts', { cache: 'force-cache' });

// Cache, but refresh in the background after an hour, and tag it:
await fetch('https://api.example.com/posts', {
  next: { revalidate: 3600, tags: ['posts'] },
});

// Never store this response:
await fetch('https://api.example.com/me', { cache: 'no-store' });

Tags let you invalidate groups of entries on demand, which is the topic of Revalidating Data.

3. The full route cache

When a route has no dynamic parts, Next.js renders it at build time (or the first time it is needed) and stores both the HTML and the RSC payload. A request for that route is then answered with the stored result and no component runs. This is what makes a static page as fast as a file. If the data cache entry behind it is revalidated, the stored route is rebuilt too. Dynamic routes are rendered for every request and are not stored here. How Next.js decides which is which is covered in Static versus Dynamic Rendering.

4. The router cache

The router cache is the only one that lives in the browser. It holds the RSC payloads of pages and layouts you have visited or that <Link> has prefetched, in memory. Going back to a page you already saw, or clicking a link to a prefetched page, can be instant because no request is needed. It is cleared when you reload the page, and entries expire after a short time. In Next.js 15 the default for page segments was reduced to zero seconds, so a link click fetches fresh data for the page, while layouts and loading states are still shared. An experimental staleTimes option in the config can raise it. router.refresh() and Server Actions that revalidate data also clear it. You saw how payloads are merged in Inside the RSC Payload.

A model of the server layers

The stand-in below puts three server layers into one tiny program. At "build time" the static route /blog is rendered and stored. Then requests arrive. Read each log line: it says which layer answered.

The routes declare what they fetch and with which mode. /account is dynamic, so it is never stored as a page, fetches /api/me twice, with no caching, and asks for /api/plans with the data cache. The model leaves out many details, but the lookup order is the real one.

Node.js Which layer answers? A model of the server caches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Follow the output. During the build, /blog fetches /api/posts twice and the second call is answered by request memoization, so only one origin call happens. Every later request for /blog is served by the full route cache with no work. The first /account request calls the origin for /api/me and /api/plans. The second /account request must run again, because it is dynamic, so /api/me goes to the origin again, but /api/plans is now a data cache hit. In total, the build and three requests needed only four origin calls.

The browser's router cache

The router cache behaves differently, because it belongs to one visitor and works on a clock. The stand-in below keeps visited pages in a cache and uses a virtual clock so you can control time. Choose a stale time, click through the links and use Wait 10 s to move time forward. Each click is answered either from the router cache or by the server (which numbers every render so you can tell).

The router cache with a stale time

With stale time 0, every visit goes to the server, which matches the Next.js 15 default for pages. With 30, a second visit within 30 seconds is instant but shows the older render number. After more than 30 virtual seconds the entry is stale and the server is called again. This is the trade-off in one screen: a longer time gives faster navigation and older data.

What changed in Next.js 15 and 16

Older tutorials (for versions 13 and 14) say that Next.js caches almost everything by default. That is no longer true.

  • Next.js 15 made fetching uncached by default. A fetch in a Server Component is no longer stored in the data cache unless you ask with cache: 'force-cache' or a revalidate option. GET Route Handlers also stopped being cached by default, and the router cache for page segments went down to zero seconds.
  • Next.js 16 added Cache Components. You turn it on with one setting, and the model flips: everything is rendered at request time unless you mark it as cacheable with the "use cache" directive. A page can then be a mix of a cached static shell and dynamic parts that stream in, a technique called partial prerendering.
TypeScript
// next.config.ts  (real Next.js 16, read-only)
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  cacheComponents: true,
};

export default nextConfig;

In the new model, caching is written next to the code that you want to cache and is visible in the source, instead of being controlled by fetch options and route segment settings that sit far from the data. The previous options remain available in the classic mode, so you will still read them in existing projects, but new projects should prefer the directive. Both ideas are explained in The use cache Directive and Cache Components.

When you read a Next.js caching article, check which version it is about. Advice from the 13/14 era ("everything is cached, opt out with no-store") is the opposite of the 15/16 default ("nothing is cached, opt in").

A practical order of thinking

  1. Is this data the same for every visitor? If yes, it can be cached on the server. If it depends on cookies or the current user, it cannot be part of a shared page.
  2. How fresh must it be? Seconds, hours or "until I change it"? That decides between a time-based lifetime and on-demand revalidation.
  3. What should invalidate it? A tag such as posts lets one mutation refresh every place that shows posts.
  4. Check the browser. Even a fresh server response can sit behind the router cache for a short time.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which cache lives in the browser?

    The router cache stores RSC payloads of visited and prefetched pages in browser memory. The other three live on the server.
  2. 2. Two components in one render call fetch the same URL with GET. Which layer prevents the second network call?

    Request memoization shares identical GET fetches within a single render pass. It lasts only for that request.
  3. 3. What is the default for a plain fetch() in a Server Component in Next.js 15 and later?

    Since Next.js 15, fetch is uncached by default. You opt in with cache: 'force-cache' or revalidate options, or, with Cache Components, with the use cache directive.
  4. 4. What does the full route cache store?

    A static route is rendered once and the HTML plus payload are stored, so later requests are answered without running components.
  5. 5. In the Cache Components model, how do you make part of a page cached?

    With cacheComponents enabled, rendering is dynamic by default and caching is opted in with "use cache" on a function, component or file.

Summary

  • Next.js has four classic caches: request memoization, the data cache, the full route cache (all on the server) and the router cache (in the browser).
  • Each has a different lifetime: one render, until revalidated, until revalidated, and until reload or expiry.
  • Since Next.js 15, fetches and GET route handlers are uncached by default, and router cache entries for pages have a zero second default.
  • Next.js 16 adds Cache Components: dynamic by default, with "use cache" marking what is cached.
  • Old articles describe the cache-everything behavior of versions 13 and 14, so always check the version.
  • Decide by asking who sees the data, how fresh it must be and what should invalidate it.

Continue with the tool that expresses caching directly in your code: The use cache Directive and Cache Components.