Skip to content

Caching

StrategyWhatWhereWhyHow longHow to refreshHow to cancel
Request Memoizationmemoize identical requests in single render passserver-side in memory (build & run time)dedupe requests & avoid props drillingshort livednot neededauto – only GET requests are memoized
Data Cachememoize any server-side fetchlocal/edge/custom storage server-sideminimize network calls & increase performancepersistent even across deploymentstime-based or on-demand revalidation{ cache: "no-store" }
Full Route Cachememoize static pages (HTML + RSC payload)localstorage/custom storage server-sideserve HTML & RSC fast for FCP & smooth hydrationpersistent across user requests & restartsrevalidation or redeployingmaking the page dynamic

Caching in Next.js

app/page.tsx
export default async function Page() {
// 默认情况下,fetch请求不会被缓存,使用next.revalidate来设置缓存时间,默认为秒
const data = await fetch("https://...", { next: { revalidate: 3600 } });
}
app/page.tsx
// 动态更新
export const dynamic = "force-dynamic";
export default async function Page() {
const data = await fetch("https://...");
}
app/page.tsx
export default async function Page() {
const data = await fetch("https://...", { cache: "no-store" });
}
app/page.tsx
import { connection } from "next/server";
export default async function Page() {
await connection();
const data = await fetch("https://...");
}

启用缓存组件之后,所有组件默认为动态内容