Skip to content
Data & CachingChapter 15 / 34Advanced5 min read

Cache Components & use cache

Learn the new Next.js use cache and Cache Components model, and how to cache pages, components and functions with cacheLife and cacheTag.

On this page

1. What are Cache Components?

In Next.js 16, everything is dynamic by default, which means it runs on every request. For the parts you want to cache, you write "use cache" yourself. The compiler builds the cache key automatically from the arguments and captured values.

This model also completes Partial Prerendering (PPR): the static and cached part of a page becomes a static shell, and the dynamic part streams in inside <Suspense>.

2. Why does it matter?

In the old App Router, caching was implicit. fetch cached itself, and developers were confused about why they saw stale data.

Old implicit cachingCache Components
fetch cached itselfNothing is cached unless you say so
A jungle of route segment configsOne directive: use cache
The whole route is static or dynamicStatic, cached and dynamic parts in one page
Hard to understand the cache keyThe compiler builds the key from arguments
A revalidate number for expirycacheLife profiles like hours, days, max

3. Where is it used?

Use caseWhat to cacheProfile
Blog posts listA component or data functionhours or max
Product catalogThe getProducts() functionhours
CMS pagesThe whole page, with cacheTagmax
Exchange ratesA data functionminutes
User cartDo not cache, stream it in SuspenseNone

4. How does it work?

use cache flow
cacheComponents true in next.config.ts
use cache in a function or component
A cache key is built from the arguments
The first time, the result is computed and stored
On the next request, the result comes from the cache
It refreshes according to cacheLife

"use cache" can be used at three levels:

LevelHowEffect
File"use cache" on the first line of the fileAll exported functions in the file are cached
ComponentThe first line of the component bodyThe rendered output of the component is cached
FunctionThe first line of an async function bodyThe return value of the function is cached

5. Code example

Step 1: enable the config

ts
import type { NextConfig } from "next";

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

export default nextConfig;

Step 2: cache a data function and a component

tsx
import { Suspense } from "react";
import { cookies } from "next/headers";
import { cacheLife, cacheTag } from "next/cache";

async function getProducts() {
  "use cache";
  cacheLife("hours");
  cacheTag("products");

  const res = await fetch("https://api.example.com/products");
  return res.json() as Promise<{ id: string; name: string }[]>;
}

async function ProductList() {
  const products = await getProducts();

  return (
    <ul>
      {products.map((product) => (
        <li key={product.id}>{product.name}</li>
      ))}
    </ul>
  );
}

async function CartCount() {
  const cartId = (await cookies()).get("cart")?.value;
  return <p>Cart: {cartId ? "has items" : "empty"}</p>;
}

export default function ShopPage() {
  return (
    <main>
      <h1>Shop</h1>
      <ProductList />
      <Suspense fallback={<p>Loading cart...</p>}>
        <CartCount />
      </Suspense>
    </main>
  );
}

How does the code work?

PartMeaning
cacheComponents: trueTurns on the new caching model
"use cache"The result of getProducts is cached
cacheLife("hours")How long the result stays fresh, a built-in profile
cacheTag("products")A tag you can use later to invalidate on demand
<h1> and ProductListPart of the static shell, available immediately
CartCountcookies() is runtime data, so it streams inside Suspense

The built-in cacheLife profiles are seconds, minutes, hours, days, weeks and max. If you do not give a profile, default is used. You can also pass an object: cacheLife({ stale: 300, revalidate: 3600, expire: 86400 }).

Interview tip

You cannot read cookies() or headers() directly inside "use cache". Read the runtime value outside and pass it to the cached function as an argument, and the value itself becomes part of the cache key.

6. Common mistakes

Mistake: Writing use cache without enabling cacheComponents

Why? The directive belongs to the new model, and without the config flag it does not work and gives a build error.

Better: Set cacheComponents to true in next.config.ts, then add use cache.

Mistake: Reading cookies or headers inside use cache

Why? A cached result is shared between all users, so runtime data is not allowed inside.

Better: Read the cookie value outside and pass it to the cached function as an argument.

Mistake: Putting user specific data in a shared cache

Why? One user's data could be shown to another user, which is a security bug.

Better: Keep personal data uncached inside Suspense, or make the user id part of the cache key.

Mistake: Adding use cache everywhere without cacheLife

Why? The default profile is applied, and it may not suit your data.

Better: Set the right cacheLife profile with every use cache.

7. What to learn before and after

Final quick revision

PointDetail
How to enablecacheComponents: true in next.config.ts
How to cache"use cache" at file, component or function level
For how longcacheLife("hours") or a custom object
TagcacheTag("products") for on-demand invalidation
Runtime dataStream it in <Suspense>, outside the cache