cacheLife
The cacheLife option allows you to define custom cache profiles when using the cacheLife function inside components or functions, and within the scope of the use cache directive.
Usage
To define a profile, enable the cacheComponents flag and add the cache profile in the cacheLife object in the next.config.js file. For example, a blog profile:
You can now use this custom blog configuration in your component or function as follows:
You can also override a built-in profile by defining one with the same name (default, seconds, minutes, hours, days, weeks, or max). See Overriding the default cache profiles.
Reference
The configuration object has key values with the following format:
| Property | Value | Description | Requirement |
|---|---|---|---|
stale | number | Duration the client should cache a value without checking the server. | Optional |
revalidate | number | Frequency at which the cache should refresh on the server; stale values may be served while revalidating. | Optional |
expire | number | Maximum duration for which a value can remain stale before switching to dynamic. | Optional - Must be longer than revalidate |
Version History
| Version | Changes |
|---|---|
v16.0.0 | cacheLife stabilized and moved from experimental.cacheLife to the top-level cacheLife option. |
Related
View related API references.
use cache
Learn how to use the "use cache" directive to cache data in your Next.js application.
cacheHandlers
Configure custom cache handlers for use cache directives in Next.js.
cacheLife
Learn how to use the cacheLife function to set the cache expiration time for a cached function or component.
Last updated