incrementalCacheHandlerPath

在 Next.js 中,預設快取處理器使用檔案系統快取。這不需要任何配置,但您可以透過在 next.config.js 中使用 incrementalCacheHandlerPath 欄位來自訂快取處理器。

next.config.js
module.exports = {
  experimental: {
    incrementalCacheHandlerPath: require.resolve('./cache-handler.js'),
  },
}

以下是一個自訂快取處理器的範例:

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 參考

快取處理器可以實作以下方法:getsetrevalidateTag

get()

參數類型描述
keystring快取值的鍵名。

回傳快取值,若找不到則回傳 null

set()

參數類型描述
keystring儲存資料的鍵名。
dataData 或 null要快取的資料。

回傳 Promise<void>

revalidateTag()

參數類型描述
tagstring要重新驗證的快取標籤。

回傳 Promise<void>。了解更多關於重新驗證資料revalidateTag() 函式的資訊。

On this page