Cache Components & use cache
Next.js 16 introduced Cache Components as the modern opt-in caching model for App Router applications.
Enable it explicitly:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfig
This changes how you should reason about prerendering, cacheable work, request-time work, and older route-level cache configuration.
What Cache Components gives youβ
The goal is not βcache every component.β
The goal is to compose three categories in one route:
static work
cached work
request-time dynamic work
A route can therefore behave like:
Page
βββ static header
βββ cached catalog
βββ cached CMS block
βββ Suspense
βββ dynamic account widget
The stable shell can be available quickly while request-time work resolves separately.
use cache directiveβ
'use cache' marks an async route, component, or function as cacheable.
Function scope:
export async function getProducts() {
'use cache'
return db.product.findMany()
}
Component scope:
export async function ProductGrid() {
'use cache'
const products = await db.product.findMany()
return <Grid products={products} />
}
File scope:
'use cache'
export async function getProducts() {
// ...
}
export async function getCategories() {
// ...
}
At file scope, exported functions must satisfy the directive's async-function constraints.
Cache the smallest meaningful domain unitβ
Avoid jumping immediately to:
'use cache'
export default async function RootLayout() { ... }
A broad cache scope can couple unrelated freshness requirements.
Prefer domain-aligned boundaries:
getPublicPlans() β weeks
getProduct(id) β hours
getInventory(id) β minutes
getCurrentUser() β dynamic/private
This makes invalidation and ownership easier to explain.
Cache key mental modelβ
A cached function's identity depends on its code and relevant inputs, including arguments and captured values.
Conceptually:
cache entry key
=
function identity
+
arguments
+
relevant closed-over values
Therefore:
getProduct('p1')
getProduct('p2')
produce separate logical entries.
This is why passing an explicit safe key into a cached function is often better than reading request state inside it.
Runtime APIs are excluded from normal use cacheβ
A normal cached function cannot directly depend on request APIs such as:
cookies()headers()- page
searchParams
Preferred pattern:
read request state outside
β
validate/normalize
β
pass safe primitive argument
β
cache shareable work
Example:
export async function getPricing(currency: string) {
'use cache'
return pricingService.list({ currency })
}
Then request code resolves the user's currency and passes it explicitly.
Authorization should stay outside shared cachesβ
Do not put this into a public shared cache:
async function getProject(projectId: string) {
'use cache'
const session = await getSession()
// authorize session here...
}
Even if you could technically structure it, authorization and caching have different responsibilities.
A safer pattern is:
request
β
identity + authorization
β
resource scope
β
cache public/shareable sub-data only
If the result is user-specific, keep it dynamic unless you have an explicit and safe personalization cache design.
Cached component outputβ
use cache can cache the output of an async component, not only raw data.
That can be useful for expensive server-rendered fragments:
export async function MarketingHero() {
'use cache'
const campaign = await getCampaign()
return <Hero campaign={campaign} />
}
The architectural question is whether the output's freshness and shareability match the cache boundary.
Static shell inclusionβ
Cache Components can include reusable cached content in the prerendered shell.
That means the distinction becomes:
stable or cacheable server work
β eligible for shell
request-dependent work
β dynamic hole
This avoids turning an entire page request-time merely because one small subtree needs runtime state.
Short-lived cachesβ
Very short cache profiles may be excluded from the prerendered shell and treated more like dynamic work.
That is appropriate: content with near-real-time freshness should not pretend to be a stable build-time shell dependency.
Randomness and time inside cachesβ
Caching freezes nondeterministic values for the cache lifetime.
export async function PromoCode() {
'use cache'
const value = crypto.randomUUID()
return <span>{value}</span>
}
Every request sharing that cache entry can see the same value until revalidation.
Likewise:
Date.now()
Math.random()
inside a cached scope describe values captured when that cache entry was generated.
If you need request-time freshness, keep that work outside the cache or use connection() for intentional runtime rendering.
Cache nested functions deliberatelyβ
You may have:
cached page
β
cached component
β
cached data function
This can be valid, but nesting creates multiple lifetimes and invalidation relationships.
Do not layer caches merely because each helper can have one.
Ask which layer owns the freshness contract.
Default cache profileβ
A use cache scope without explicit cacheLife uses the default profile.
Current defaults include a client stale period and server revalidation behavior.
For production code, explicit profiles are often easier to audit:
'use cache'
cacheLife('hours')
A reviewer should be able to understand freshness without tracing framework defaults.
Cache Components vs React cacheβ
React cache()
β server render/request memoisation
'use cache'
β Next.js reusable cache entry with lifetime/revalidation behavior
Use React cache() to deduplicate repeated work in one rendering lifecycle.
Use use cache when you want reusable server output across eligible requests/renderings.
Cache Components vs force-cache fetchβ
fetch(..., { cache: 'force-cache' })
β cache HTTP response
'use cache'
async function getData() { ... }
β cache function/component output
A use cache function can include:
- database reads
- external API calls
- transformations
- computed data
- component output
This makes it useful beyond HTTP sources.
Partial Prerendering mental modelβ
Think in terms of a route shell:
build / prerender pass
β
static + cacheable work becomes shell
β
dynamic boundaries remain holes
β
request arrives
β
dynamic work completes/streams
Do not frame Partial Prerendering as a separate old experimental feature in modern Next.js 16. Cache Components is the current model that exposes this composition.
Suspense boundaries matterβ
Dynamic work should be enclosed at meaningful Suspense boundaries so the stable shell can render independently.
Bad:
Page
βββ one huge dynamic subtree
Better:
Page shell
βββ cached catalog
βββ cached recommendations
βββ Suspense
βββ user-specific cart
Boundary placement is both a rendering and UX decision.
Static export limitationβ
use cache under Cache Components is not a universal static-export substitute.
Platform support depends on deployment mode, and static export does not support the runtime caching model in the same way as a server deployment.
Treat deployment target as part of cache architecture.
Common mistakesβ
Adding use cache everywhereβ
More cache scopes mean more keys, invalidation paths, and mental overhead.
Caching authorization results broadlyβ
Permission state is often request/user specific and security-sensitive.
Hiding request dependencies inside utilitiesβ
Make request dependence explicit at boundaries.
Depending on default lifetime silentlyβ
Explicit profiles communicate intent better for important data.
Caching nondeterminism accidentallyβ
Time/randomness becomes stable for the cache entry lifetime.
Debugging checklistβ
If use cache behaves unexpectedly:
- confirm
cacheComponents: true - confirm directive placement
- inspect arguments and closed-over values
- inspect runtime API usage
- inspect explicit/default
cacheLife - inspect tags and invalidation
- inspect nested cache boundaries
- verify deployment cache persistence assumptions
- compare serverless vs long-lived server behavior
- test production build/prerender output
Interview questionsβ
What does use cache cache?
The output of an async function/component/route scope under the Cache Components model, keyed by its identity and relevant inputs.
Can use cache read cookies directly?
Normal use cache should not directly access runtime request APIs. Read them outside and pass safe values as arguments.
Why is cached component output useful?
It lets expensive reusable server-rendered subtrees participate in the prerendered shell rather than caching only raw HTTP responses.
How does Cache Components relate to Partial Prerendering?
Cache Components is the modern Next.js 16 programming model for combining prerendered shell, reusable cached work, and request-time holes.
Exerciseβ
Refactor a product page into:
static navigation
cached product details
cached review aggregate
dynamic user price entitlement
dynamic cart
Choose cache scope, profile, tags, Suspense boundaries, and security ownership for each subtree.