跳至內容
API 參考函式unstable_noStore

unstable_noStore

此 API 目前不穩定,可能會有所變動。

此 API 將在未來棄用。在版本 15 中,建議使用 connection 取代 unstable_noStore

unstable_noStore 可用於宣告式地停用靜態渲染,並指示特定組件不應被快取。

import { unstable_noStore as noStore } from 'next/cache';
 
export default async function ServerComponent() {
  noStore();
  const result = await db.query(...);
  ...
}

注意事項:

  • unstable_noStore 等同於在 fetch 上使用 cache: 'no-store'
  • unstable_noStoreexport const dynamic = 'force-dynamic' 更受青睞,因為它更精細,並且可以在每個組件上單獨使用。
  • unstable_cache 內使用 unstable_noStore 不會停用靜態生成。相反,它會遵從快取設定來決定是否快取結果。

用法

如果您不想傳遞額外的選項給 fetch,例如 cache: 'no-store'next: { revalidate: 0 },或者在 fetch 不可用時,您可以使用 noStore() 作為所有這些用例的替代方案。

import { unstable_noStore as noStore } from 'next/cache';
 
export default async function ServerComponent() {
  noStore();
  const result = await db.query(...);
  ...
}

版本歷史記錄

版本變更
v14.0.0引入 unstable_noStore