Next.jsMedium

How do you opt out of caching in the App Router?

With Cache Components, nothing is cached unless you add "use cache", so opting out means simply not caching and putting request-time work inside <Suspense>. Use await connection() to force request-time execution for code that reads no request data. In the previous model, use cache: 'no-store', revalidate = 0 or dynamic = 'force-dynamic'.

Cache Components

  • Uncached data: a plain fetch or database call is request-time; wrap the component in <Suspense>.
  • Request APIs (cookies(), headers(), searchParams) are request-time automatically.
  • await connection() before Math.random(), Date.now() or synchronous drivers to keep them out of the prerender.
  • Short cacheLife: revalidate: 0 or expire under 5 minutes turns a cache into a dynamic hole.
  • navigation() / prefetch() (16.4) defer expensive subtrees out of the App Shell without making them uncached.
  • prefetch = 'force-disabled' segment config stops prefetching a route.
  • Draft Mode bypasses all use cache entries automatically.

Previous model

  • fetch(url, { cache: 'no-store' }) or { next: { revalidate: 0 } } for one request.
  • export const dynamic = 'force-dynamic' or export const revalidate = 0 for a whole route.
  • export const fetchCache = 'force-no-store' for all fetches in a segment.
  • unstable_noStore() (replaced by connection()).

Client side

  • router.refresh() to discard client cache for the current route.
  • staleTimes or a short cacheLife stale value to shorten client reuse.

Route Handlers and HTTP

Set Cache-Control: private, no-store on responses that CDNs must not cache.

Code example

tsx
1// app/live/page.tsx (Cache Components)
2import { Suspense } from "react";
3import { connection } from "next/server";
4
5async function LiveScore() {
6 // Not cached: plain fetch, runs per request
7 const score = await fetch("https://api.example.com/match/42").then((r) => r.json());
8 return <p>{score.home} - {score.away}</p>;
9}
10
11async function Ticket() {
12 await connection(); // request-time even though no request data is read
13 return <p>Ticket #{crypto.randomUUID().slice(0, 8)}</p>;
14}
15
16export default function Page() {
17 return (
18 <main>
19 <h1>Live match</h1>
20 <Suspense fallback={<p>Loading score…</p>}>
21 <LiveScore />
22 <Ticket />
23 </Suspense>
24 </main>
25 );
26}
27
28// Previous model equivalents:
29// export const dynamic = "force-dynamic";
30// await fetch(url, { cache: "no-store" });

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