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()
関数について詳しく学べます。