use cache directive to control what gets cached and for how long.
This page covers caching with Cache Components, enabled by setting
cacheComponents: true in your next.config.ts. For the previous model, see the Caching and Revalidating (Previous Model) guide.Enabling Cache Components
The use cache directive
The use cache directive caches the return value of async functions and components. Apply it at two levels:
- Data-level: Cache a function that fetches or computes data
- UI-level: Cache an entire component or page
Data-level caching
UI-level caching
If you add
'use cache' at the top of a file, all exported functions in the file will be cached.cacheLife profiles
cacheLife controls how long cached data remains valid. It accepts a profile name or a custom configuration object:
For fine-grained control, pass a configuration object:
Streaming uncached data
For components that require fresh data on every request, do not use"use cache". Wrap them in <Suspense> instead — React renders the fallback immediately and streams in the resolved content:
Working with runtime APIs
Runtime APIs (cookies, headers, searchParams) are only available at request time. Components that access them should be wrapped in <Suspense>:
Passing runtime values to cached functions
Extract values from runtime APIs and pass them as arguments to cached functions:How rendering works
At build time, Next.js renders your route’s component tree. How each component is handled depends on the APIs it uses:use cache: the result is cached and included in the static shell<Suspense>: the fallback UI is included in the static shell; content streams at request time- Deterministic operations (pure computations, module imports): automatically included in the static shell
Complete example
Here’s how static content, cached dynamic content, and streaming dynamic content work together:Opting out of the static shell
Placing an empty<Suspense> fallback above the document body causes the entire app to defer to request time:
