Skip to content
App Router...next.config.js OptionsincrementalCacheHandlerPath

incrementalCacheHandlerPath

In Next.js, the default cache handler uses the filesystem cache. This requires no configuration, however, you can customize the cache handler by using the incrementalCacheHandlerPath field in next.config.js.

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

Here's an example of a custom cache handler:

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 Reference

The cache handler can implement the following methods: get, set, and revalidateTag.

get()

ParameterTypeDescription
keystringThe key to the cached value.

Returns the cached value or null if not found.

set()

ParameterTypeDescription
keystringThe key to store the data under.
dataData or nullThe data to be cached.

Returns Promise<void>.

revalidateTag()

ParameterTypeDescription
tagstringThe cache tag to revalidate.

Returns Promise<void>. Learn more about revalidating data or the revalidateTag() function.