Skip to content

use cache and Cache Components

Cache Components flips the model: instead of remembering per-fetch cache options, you mark a function, file, or component with the 'use cache' directive to say “cache this,” then shape its lifetime and invalidation with cacheLife and cacheTag.

Add the string 'use cache' at the top of a function (or file) and everything it returns is cached — a fetch, a database query, or a whole rendered component. It replaces scattering cache options across individual calls.

lib/data.ts
import { cacheLife, cacheTag } from 'next/cache'
async function getData() {
'use cache'
cacheLife('hours')
cacheTag('data')
const res = await fetch('https://api.example.com/data')
return res.json()
}

cacheLife sets how long the entry stays fresh — a named profile like 'hours' or an explicit object. cacheTag attaches a tag so the same revalidateTag you already know invalidates it on demand.

import { cacheLife, cacheTag } from 'next/cache'
async function getProduct(id: string) {
'use cache'
cacheTag(`product-${id}`)
cacheLife({ stale: 60, revalidate: 300 })
return db.product.findUnique({ where: { id } })
}

Turning it on: cacheComponents and Partial Prerendering

Section titled “Turning it on: cacheComponents and Partial Prerendering”

'use cache' requires the cacheComponents flag. In Next.js 16 that same flag is also how you opt into Partial Prerendering — the older experimental ppr flag and experimental_ppr export were removed. With it on, Next.js prerenders a static shell and streams the dynamic, uncached holes in at request time.

next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfig

Two variants extend the model. 'use cache: private' caches per user — it may read cookies(), so it is never shared across visitors. 'use cache: remote' stores in a shared server-side cache across users. These are current but still stabilizing, so pin your Next.js version and re-check the docs before relying on exact names.

import { cacheLife } from 'next/cache'
import { cookies } from 'next/headers'
async function getRecommendations() {
'use cache: private'
cacheLife({ stale: 60 })
const sessionId = (await cookies()).get('session-id')?.value
return db.recommendations.forSession(sessionId)
}
flowchart TD
  A["cacheComponents: true"] --> B["'use cache' function or component"]
  A --> C["Partial Prerendering"]
  B --> D["Cached static shell"]
  C --> D
  D --> E["Dynamic uncached holes stream in"]
cacheComponents: the static shell plus streamed dynamic holes
What does the use cache directive do?
Which config flag enables use cache?
What set the lifetime and tag of a use cache entry?
In Next.js 16, what does cacheComponents also enable?