All posts
nextjsreact

Next.js 16 Cache Components: Caching You Opt Into, One Function at a Time

Next.js 15 stopped caching by default. Next.js 16 gave the opt-in a real shape, `use cache` on the functions and components that deserve it, and Suspense around everything else.

Muhamad Choirul Adam 3 min readPhoto: Unsplash

A year ago I wrote that Next.js 15 stopped caching by default, and that this was the fix. It was, but it left a gap: once nothing was cached, opting back in was a mix of route segment config, fetch options, and unstable_cache.

Next.js 16 shipped in October, and Cache Components fills that gap. This site runs on it.

One mental model

With cacheComponents turned on, every page is a mix of two kinds of work:

  • Cached work, marked with "use cache". It runs once, the result is reused, and it can be part of the static shell.
  • Dynamic work, anything that reads the request, like cookies or headers. It must sit inside a <Suspense> boundary, so it streams in after the shell.
Rendering diagram…

If you read the request outside Suspense, or fetch data without marking it either way, the build tells you. That error was annoying for a day and useful every day after.

What it looks like

next.config.ts
const nextConfig: NextConfig = {
  cacheComponents: true,
};
lib/products.ts
import { cacheLife, cacheTag } from "next/cache";
 
export async function getProducts(category: string) {
  "use cache";
  cacheLife("hours");
  cacheTag(`products-${category}`);
  return db.query.products.findMany({ where: eq(products.category, category) });
}
app/shop/page.tsx
export default function ShopPage() {
  return (
    <>
      <ProductGrid category="shoes" />
      <Suspense fallback={<CartSkeleton />}>
        <Cart />
      </Suspense>
    </>
  );
}

The product grid is cached and part of the shell. The cart reads a cookie, so it streams. When an admin edits a product, one revalidateTag call refreshes exactly the lists that use it.

The pieces

APIWhat it does
"use cache"Caches a function or component; arguments become the key
cacheLife(profile)How long it stays fresh: "minutes", "hours", "days"
cacheTag(tag)Labels the entry so you can invalidate it later
revalidateTag(tag)Marks every entry with that tag as stale

What took getting used to

  • Arguments are the cache key. Passing a whole object when you only need an id makes the key bigger and the cache less useful.
  • Request data can't go inside. A cached function can't read cookies. Read them outside and pass in what you need.
  • Middleware is proxy.ts now. The rename is cosmetic, but it's a good reminder that it's not where access checks belong.

Why it matters

Caching bugs are the worst kind: the page works, it's just wrong for some users some of the time. What I like about Cache Components is that the decision is written next to the code it affects. When I read a function, I can see whether it's cached, for how long, and what invalidates it. No config file three folders away.