HTML 原文 · 完整 Markdown · 纯文本 · 分段目录 · ← 上一段 · 下一段 →

03-第 2 层:数据缓存 (Data Cache)

第 5 / 22 段:三种缓存策略

Next.js 缓存机制全解析 · 发布 2026-02-20 · 更新 2026-11-11 · 标签:Next.js、缓存、前端架构、React

三种缓存策略

1. 永久缓存

// 显式启用永久缓存
const res = await fetch("https://api.example.com/posts", {
  cache: "force-cache",
});

注意:在 Next.js 13/14 中,force-cache 是 fetch 的默认行为。但在 Next.js 15+ 中,默认行为已改为 no-store(不缓存),需要显式声明 force-cache 才会启用永久缓存。

效果:类似 SSG(静态站点生成)

graph LR
    Build[构建时] --> Fetch[请求 API]
    Fetch --> Store[存储到文件]
    Store --> Forever[永久使用<br/>直到重新构建]

    style Forever fill:#9f9,stroke:#333,stroke-width:2px

适用场景:


2. 从不缓存(Next.js 15+ 默认)

// Next.js 15+ 的默认行为,也可以显式声明
const res = await fetch("https://api.example.com/posts", {
  cache: "no-store",
});

效果:类似 SSR(服务端渲染)

graph LR
    U1[用户1请求] --> API[请求 API]
    U2[用户2请求] --> API
    U3[用户3请求] --> API

    style API fill:#f99,stroke:#333,stroke-width:2px

适用场景:


3. 定时缓存(推荐)