unstable_noStore

unstable_noStore 可用于声明式地退出静态渲染,表明特定组件不应被缓存。

import { unstable_noStore as noStore } from 'next/cache';

export default async function Component() {
  noStore();
  const result = await db.query(...);
  ...
}

须知:

  • unstable_noStore 等效于在 fetch 中使用 cache: 'no-store'
  • 相比 export const dynamic = 'force-dynamic',更推荐使用 unstable_noStore,因为它更细粒度且可以基于单个组件使用
  • unstable_cache 中使用 unstable_noStore 不会退出静态生成,而是会遵从缓存配置来决定是否缓存结果。

用法

如果您不想向 fetch 传递额外选项(如 cache: 'no-store'next: { revalidate: 0 }),可以用 noStore() 替代所有这些用例。

import { unstable_noStore as noStore } from 'next/cache';

export default async function Component() {
  noStore();
  const result = await db.query(...);
  ...
}

版本历史

版本变更描述
v14.0.0引入 unstable_noStore 功能

On this page