Next.jsMedium

What is next: { revalidate } in the fetch options?

next: { revalidate: N } caches a fetch response in the Data Cache and refreshes it at most every N seconds with stale-while-revalidate. It is time-based ISR for a single request. revalidate: 0 disables caching and false caches indefinitely.

Values

  • Positive number: cache, then regenerate in the background after N seconds.
  • 0: never cache (same as cache: 'no-store'); the route becomes dynamic.
  • false: cache indefinitely until on-demand revalidation (same as Infinity).

Interactions

  • The lowest revalidate among the fetches and segment configs of a route sets how often the whole prerendered route regenerates.
  • Combine with next: { tags: [...] } for on-demand invalidation.
  • Conflicting options such as { cache: 'no-store', next: { revalidate: 60 } } log a warning, and no-store wins.

Stale-while-revalidate in practice

With revalidate: 60, a request at second 61 still gets the cached response, and triggers a refresh. The next request gets the new data. If the refresh fails, the old data stays.

With Cache Components

These fetch options belong to the previous model. With cacheComponents: true, the equivalent is a "use cache" function with cacheLife({ revalidate: 60 }) or a preset profile. cacheLife also controls the browser stale time and the expire time, which next.revalidate cannot.

Code example

tsx
1// app/weather/page.tsx - previous caching model
2export default async function Weather() {
3 const res = await fetch("https://api.example.com/weather?city=Delhi", {
4 next: { revalidate: 600, tags: ["weather"] }, // at most every 10 minutes
5 });
6 const data: { tempC: number; updatedAt: string } = await res.json();
7
8 return (
9 <p>
10 {data.tempC}°C (updated {data.updatedAt})
11 </p>
12 );
13}
14
15// Cache Components equivalent:
16// import { cacheLife, cacheTag } from "next/cache";
17// async function getWeather() {
18// "use cache";
19// cacheLife({ stale: 300, revalidate: 600, expire: 86400 });
20// cacheTag("weather");
21// return fetch("https://api.example.com/weather?city=Delhi").then((r) => r.json());
22// }

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