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