unstable_noStore
這是一個舊版的 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_noStore
比export 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(...);
...
}
版本歷史
版本 | 變更 |
---|---|
v15.0.0 | unstable_noStore 已棄用,改用 connection 。 |
v14.0.0 | 引入 unstable_noStore 。 |
這有幫助嗎?