unstable_noStore

在版本 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'
  • 相較於 export const dynamic = 'force-dynamic',建議優先使用 unstable_noStore,因為它更細粒度且能以元件為單位使用
  • 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(...);
  ...
}

版本歷史

版本變更內容
v15.0.0unstable_noStore 被棄用,改用 connection
v14.0.0unstable_noStore 首次引入。

On this page