Skip to content
next.js questions

Next.js · 5 of 10senior~1 min

How does caching work in Next.js 16 with Cache Components?

#caching#performance

Cache Components

With cacheComponents: true caching is explicit: nothing is cached unless you ask for it with the 'use cache' directive.

ts
import { cacheLife, cacheTag } from 'next/cache'; export async function getProducts() { 'use cache'; cacheLife('hours'); cacheTag('products'); return db.query('SELECT * FROM products'); }

'use cache' can be placed in a data function, in a component or at the top of a file. Arguments and captured variables become the cache key, so different inputs get separate entries.

Three kinds of content on a page

ContentHowWhere it ends up
Static (no I/O)Nothing to doPrerendered static shell
Cached data'use cache' + cacheLifeStatic shell, revalidated in the background
Request data (cookies(), uncached fetch)Wrap in <Suspense>Streams in at request time

cacheLife profiles

cacheLife sets stale (client), revalidate (background refresh) and expire (hard limit). Profiles: seconds, minutes, hours, days, weeks, max or a custom object. Very short lifetimes are excluded from the prerender and become dynamic.

Variants

  • 'use cache: private' - may read cookies() / headers(), cached per user.
  • 'use cache: remote' - durable shared cache handler instead of in-memory (useful on serverless).

How it differs from earlier versions

  • Next.js 14: fetch was cached by default, which surprised many people.
  • Next.js 15: fetch and GET Route Handlers stopped being cached by default.
  • Next.js 16: 'use cache' makes caching opt-in and works for any async code, not only fetch.

Values like Date.now() or Math.random() must be either cached or read after await connection() inside <Suspense>, otherwise the dev overlay reports a blocking prerender.

updated

did you know it?

read more on the blog

view all

related questions