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
fetchor database call is request-time; wrap the component in<Suspense>. - Request APIs (
cookies(),headers(),searchParams) are request-time automatically. await connection()beforeMath.random(),Date.now()or synchronous drivers to keep them out of the prerender.- Short
cacheLife:revalidate: 0orexpireunder 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 cacheentries automatically.
Previous model
fetch(url, { cache: 'no-store' })or{ next: { revalidate: 0 } }for one request.export const dynamic = 'force-dynamic'orexport const revalidate = 0for a whole route.export const fetchCache = 'force-no-store'for all fetches in a segment.unstable_noStore()(replaced byconnection()).
Client side
router.refresh()to discard client cache for the current route.staleTimesor a shortcacheLifestalevalue 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";45async function LiveScore() {6 // Not cached: plain fetch, runs per request7 const score = await fetch("https://api.example.com/match/42").then((r) => r.json());8 return <p>{score.home} - {score.away}</p>;9}1011async function Ticket() {12 await connection(); // request-time even though no request data is read13 return <p>Ticket #{crypto.randomUUID().slice(0, 8)}</p>;14}1516export 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}2728// 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.