| `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 fetching; must be longer than `revalidate`. | Optional - Must be longer than `revalidate` |
| `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 fetching; must be longer than `revalidate`. | Optional - Must be longer than `revalidate` |
The "stale" property differs from the [`staleTimes`](/docs/app/api-reference/next-config-js/staleTimes) setting in that it specifically controls client-side router caching. While `staleTimes` is a global setting that affects all instances of both dynamic and static data, the `cacheLife` configuration allows you to define "stale" times on a per-function or per-route basis.
> **Good to know**: The “stale” property does not set the `Cache-control: max-age` header. It instead controls the client-side router cache.
> **Good to know**: The “stale” property does not set the `Cache-control: max-age` header. It instead controls the client-side router cache.
### Default cache profiles
Next.js provides a set of named cache profiles modeled on various timescales. If you don'tspecifyacacheprofileinthe`cacheLife`functionalongsidethe`use cache`directive,Next.jswillautomaticallyapplythe“default”cacheprofile.
And in your `page.tsx` file you can add the `usecache` directive to the top of the file, and define a cache profile:
```tsxfilename="app/page.tsx"
...
...
@@ -341,24 +342,24 @@ export default Page() {
### Caching component output with `usecache`
You can use `usecache` at the component level to cache any fetches or computations performed within that component. When you reuse the component throughout your application it can share the same cache entry as long as the props maintain the same structure.
You can use `usecache` at the component level to cache any fetches or computations performed within that component. When you reuse the component throughout your application it can share the same cache entry as long as the props maintain the same structure.
The props are serialized and form part of the cache key. If you use the same component in multiple places in your application, the cache entry will be reused as long as the serialized props produce the same value in each instance.
Since you can add `usecache` to any asynchronous function you aren't limited to caching components or routes only. You might want to cache a network request or database query or compute something that is very slow. By adding `usecache` to a function containing this type of work it becomes cacheable, and when reused, will share the same cache entry.
Since you can add `usecache` to any asynchronous function you aren't limited to caching components or routes only. You might want to cache a network request or database query or compute something that is very slow. By adding `usecache` to a function containing this type of work it becomes cacheable, and when reused, will share the same cache entry.
@@ -15,15 +15,15 @@ It is expected to be used in conjunction with [`use cache`](/docs/app/api-refere
To enable the `dynamicIO` flag, set it to `true` in the `experimental` section of your `next.config.ts` file:
```ts filename="next.config.ts"
import type { NextConfig } from 'next';
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
dynamicIO: true,
}
};
export default nextConfig;
},
}
export default nextConfig
```
When `dynamicIO` is enabled, you can use the following cache functions and configurations:
...
...
@@ -34,4 +34,4 @@ When `dynamicIO` is enabled, you can use the following cache functions and confi
## Notes
- While `dynamicIO` can optimize performance by ensuring fresh data fetching during runtime, it may also introduce additional latency compared to serving pre-rendered content.
\ No newline at end of file
- While `dynamicIO` can optimize performance by ensuring fresh data fetching during runtime, it may also introduce additional latency compared to serving pre-rendered content.