incrementalCacheHandlerPath
在 Next.js 中,預設快取處理器使用檔案系統快取。這不需要任何配置,但您可以透過在 next.config.js
中使用 incrementalCacheHandlerPath
欄位來自訂快取處理器。
module.exports = {
experimental: {
incrementalCacheHandlerPath: require.resolve('./cache-handler.js'),
},
}
以下是一個自訂快取處理器的範例:
const cache = new Map()
module.exports = class CacheHandler {
constructor(options) {
this.options = options
this.cache = {}
}
async get(key) {
return cache.get(key)
}
async set(key, data) {
cache.set(key, {
value: data,
lastModified: Date.now(),
})
}
}
API 參考
快取處理器可以實作以下方法:get
、set
和 revalidateTag
。
get()
參數 | 類型 | 描述 |
---|---|---|
key | string | 快取值的鍵名。 |
回傳快取值,若找不到則回傳 null
。
set()
參數 | 類型 | 描述 |
---|---|---|
key | string | 儲存資料的鍵名。 |
data | Data 或 null | 要快取的資料。 |
回傳 Promise<void>
。
revalidateTag()
參數 | 類型 | 描述 |
---|---|---|
tag | string | 要重新驗證的快取標籤。 |
回傳 Promise<void>
。了解更多關於重新驗證資料或 revalidateTag()
函式的資訊。