Next.jsMedium

What is the fetch API extension in Next.js?

On the server, Next.js extends the native fetch so each request can declare caching behavior with cache and next: { revalidate, tags }, and identical requests are memoized within a render. The defaults changed over time, and with Cache Components the recommended way to cache is "use cache" instead of fetch options.

Request memoization

During a single render pass, fetch calls with the same URL and options run once and share the result, so a layout and page can both call getUser() without double requests. This applies to GET and HEAD, not to Route Handlers, and only lasts for that request.

Caching options (previous model)

  • cache: 'force-cache' caches the response in the Data Cache.
  • cache: 'no-store' always fetches fresh data.
  • next: { revalidate: 60 } caches with time-based revalidation.
  • next: { tags: ['posts'] } lets revalidateTag('posts', 'max') invalidate it.

Since Next.js 15, the default is not cached. In Next.js 14 the default was force-cache, which surprised many developers.

With Cache Components (Next.js 16)

  • An uncached fetch is treated as request-time I/O: wrap the component in <Suspense>.
  • To cache it, call fetch inside a function marked "use cache" and set cacheLife and cacheTag.
  • fetch cache entries persist across deployments, while use cache entries are scoped to the build. Use the fetch cache or unstable_cache if data must survive deploys.

Limits

fetch options have no caching effect in proxy.ts. Third-party SDKs that use their own HTTP client do not get these extensions; use "use cache" or React.cache around them.

Code example

tsx
1// lib/api.ts
2
3// Previous model: caching via fetch options
4export async function getCategories() {
5 const res = await fetch("https://api.example.com/categories", {
6 next: { revalidate: 3600, tags: ["categories"] },
7 });
8 return res.json();
9}
10
11export async function getStockLevel(sku: string) {
12 const res = await fetch(`https://api.example.com/stock/${sku}`, { cache: "no-store" });
13 return res.json();
14}
15
16// Cache Components: plain fetch inside a cached function
17import { cacheLife, cacheTag } from "next/cache";
18
19export async function getCategoriesCached() {
20 "use cache";
21 cacheLife("hours");
22 cacheTag("categories");
23 const res = await fetch("https://api.example.com/categories");
24 return res.json();
25}
26
27// Memoization: both calls in one render hit the network once
28// await getCategories(); await getCategories();

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