Skip to content

Caching

@cloudflare/next-on-pages supports caching and revalidating data returned by subrequests you make in your app by calling fetch().

By default, all fetch() subrequests made in your Next.js app are cached. Refer to the Next.js documentation for information about how to disable caching for an individual subrequest, or for an entire route.

The cache persists across deployments. You are responsible for revalidating/purging this cache.

Storage options

You can configure your Next.js app to write cache entries to and read from either Workers KV or the Cache API.

It takes an extra step to enable, but Cloudflare recommends caching data using Workers KV.

When you write cached data to Workers KV, you write to storage that can be read by any Cloudflare location. This means your app can fetch data, cache it in KV, and then subsequent requests anywhere around the world can read from this cache.

To use Workers KV as the cache for your Next.js app, add a KV binding to your Pages project, and set the name of the binding to __NEXT_ON_PAGES__KV_SUSPENSE_CACHE.

Cache API (default)

The Cache API is the default option for caching data in your Next.js app. You do not need to take any action to enable the Cache API.

In contrast with Workers KV, when you write data using the Cache API, data is only cached in the Cloudflare location that you are writing data from.