Cache life profiles
What you need to know
A cache lifetime describes several distinct clocks. cacheLife lets you define how long a client may reuse a result, when server-side refresh becomes eligible and when old server data must no longer be served under the profile.
By the end, you can: distinguish stale, revalidate and expire; choose freshness requirements.
Set a custom lifetime in a cached function
import { cacheLife } from 'next/cache';export async function publicEdition() { 'use cache'; cacheLife({ stale: 60, revalidate: 300, expire: 1800 }); return { title: 'Fern Atlas', edition: 2 };}Expected behavior: The public result has three distinct lifetime settings; the client may still navigate using its own cache rules.
How it works
The three values are expressed in seconds. Stale describes client router reuse, not a CDN Cache-Control header. Revalidate describes when a server lookup can serve existing data and initiate regeneration; expire defines the point at which the server must obtain a fresh result instead of returning the expired entry. Time does not mean a scheduled job runs exactly on the threshold. Choose a valid profile for the installed release, with expire greater than revalidate; the age classifier below assumes a valid profile and a nonnegative age. A failed refresh is not proof of a new value. Demand, nested scopes and platform behavior matter. Use one explicit profile per cached scope to avoid implicit lifetime interactions. A custom profile is useful when several public readers share a freshness requirement; an inline object is useful for a special case. For the teaching model use stale 60, revalidate 300 and expire 1800. A server access at age 299 is fresh, at 300 it may serve the existing result and refresh, and at 1800 it must refresh before returning. These examples intentionally ignore browser reuse, failures and concurrent refreshes; they do not simulate the complete framework. Short cache profiles can affect prerender eligibility, so verify their behavior with the exact selected patch.
Classify server age in an explicitly simplified model
function classify(age) { if (age >= 1800) return 'block'; if (age >= 300) return 'serve-and-refresh'; return 'fresh';}console.log([299,300,1799,1800].map(classify));Expected behavior: fresh, serve-and-refresh, serve-and-refresh, block. This is a pure age classifier, not an actual cache implementation.
Common mistake
Do not read stale as the server TTL or assume a refresh happens without a request. Also do not wrap a short-lived widget in a long-lived cached output and expect every inner read to remain immediately visible.
Practice and verification
Run the staged age classifier exercise. Design a real test with controlled origin revisions, fresh reads and time boundaries; inspect client navigation separately from direct server access. Explain why a checkout price should be independently validated.
Scope: App Router, proposed Next.js 16.x and React 19, Cache Components enabled in a separate fixture. Framework snippets are staged and not executed; local pure models are not framework evidence. Exact patched dependencies and platform contracts are pending.
Further reading: Life · Revalidating