incrementalCacheHandlerPath

Next.js では、デフォルトのキャッシュハンドラはファイルシステムキャッシュを使用します。これは設定不要ですが、next.config.jsincrementalCacheHandlerPath フィールドを使用してキャッシュハンドラをカスタマイズできます。

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 リファレンス

キャッシュハンドラは以下のメソッドを実装できます: get, set, revalidateTag

get()

パラメータ説明
keystringキャッシュ値へのキー

キャッシュされた値、または見つからない場合は null を返します。

set()

パラメータ説明
keystringデータを保存するキー
dataData または nullキャッシュするデータ

Promise<void> を返します。

revalidateTag()

パラメータ説明
tagstring再検証するキャッシュタグ

Promise<void> を返します。データの再検証またはrevalidateTag() 関数について詳しく学べます。