You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 14开启output:"export"时,如何禁用静态站点构建缓存?

Next.js 14 App Router静态导出时API数据缓存问题的解决办法

一、强制构建时拉取最新数据(禁用缓存)

在App Router里,fetch默认会缓存请求结果,导致静态导出时用旧数据。解决这个问题直接在请求里加配置或者用缓存控制函数:

  • 给fetch加cache: 'no-store':每次构建都会重新请求API,完全跳过缓存。代码示例:
async function getData() {
  const res = await fetch('你的API地址', { cache: 'no-store' });
  if (!res.ok) {
    throw new Error('获取数据失败');
  }
  return res.json();
}

export default async function Page() {
  const data = await getData();
  return <div>{/* 渲染你的数据 */}</div>;
}
  • Server Component里用unstable_noStore:如果是服务端组件,直接调用这个函数就能禁用该组件的缓存,确保构建时拿新数据:
import { unstable_noStore as noStore } from 'next/cache';

async function getData() {
  noStore(); // 关掉缓存
  const res = await fetch('你的API地址');
  return res.json();
}

二、定期重新构建更新静态文件

虽然output: "export"模式不支持增量静态再生(ISR)的自动刷新,但你可以定期执行next build命令,每次构建都会重新拉取最新数据生成新的静态文件,替换out目录里的旧内容。

三、清理残留缓存再构建

如果之前的缓存文件残留导致数据没更新,先删了.next目录再构建,保证环境干净:

rm -rf .next && next build

内容的提问来源于stack exchange,提问作者Joe Seifi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 14:40:13