Next.jsMedium

How do you set a revalidation time in ISR?

With Cache Components, call cacheLife inside a "use cache" scope, using a preset profile such as 'hours' or an object with stale, revalidate and expire. Without Cache Components, use export const revalidate = N or fetch(url, { next: { revalidate: N } }). In the Pages Router, return revalidate: N from getStaticProps.

Cache Components (Next.js 16+)

cacheLife profiles set three timings:

  • stale: how long the browser may reuse the data without asking the server (minimum 30 seconds on the client).
  • revalidate: after this, the next request serves cached content and regenerates in the background.
  • expire: after this long with no traffic, the next request waits for fresh content.

Presets: seconds, minutes, hours (revalidate 1h), days (1d), weeks (1w), max (30d), and default (revalidate 15 minutes) if you call no cacheLife. Define custom named profiles in next.config.ts under cacheLife.

Previous model

  • export const revalidate = 3600 on a page or layout sets the route default. It must be a literal number, not an expression like 60 * 60.
  • fetch(url, { next: { revalidate: 3600 } }) sets it per request.
  • The lowest value in a route wins.
  • revalidate = 0 makes the route dynamic.

Choosing a value

Match the profile to how often content changes, and prefer a long lifetime plus on-demand revalidateTag for CMS content. That way pages update right after an edit instead of waiting for a timer.

Short lifetimes

A revalidate of 0, or expire under 5 minutes (as in the seconds profile), excludes the cache from prerendering: it becomes a dynamic hole that needs <Suspense>.

Code example

tsx
1// lib/data.ts - Cache Components
2import { cacheLife } from "next/cache";
3
4export async function getProducts() {
5 "use cache";
6 cacheLife("hours"); // stale 5m, revalidate 1h, expire 1d
7 return fetch("https://api.example.com/products").then((r) => r.json());
8}
9
10export async function getExchangeRates() {
11 "use cache";
12 cacheLife({ stale: 60, revalidate: 300, expire: 3600 }); // custom inline profile
13 return fetch("https://api.example.com/rates").then((r) => r.json());
14}
15
16// next.config.ts - reusable custom profile
17// cacheLife: { editorial: { stale: 600, revalidate: 3600, expire: 86400 } }
18
19// Previous model (no cacheComponents):
20// export const revalidate = 3600;
21// await fetch(url, { next: { revalidate: 3600 } });
22
23// Pages Router:
24// return { props, revalidate: 3600 };

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app