> The Data Cache is a persistent [HTTP cache](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching). Depending on your platform, the cache can scale automatically and be [shared across multiple regions](https://vercel.com/docs/infrastructure/data-cache).
> The Data Cache is a persistent [HTTP cache](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching). Depending on your platform, the cache can scale automatically and be [shared across multiple regions](https://vercel.com/docs/infrastructure/data-cache).
>
>
> > Learn more about the [Data Cache](/docs/app/building-your-application/caching#data-cache).
> Learn more about the [Data Cache](/docs/app/building-your-application/caching#data-cache).
### Revalidating Data
### Revalidating Data
...
@@ -143,7 +143,7 @@ export default async function Page() {
...
@@ -143,7 +143,7 @@ export default async function Page() {
If using a Route Handler, you should create a secret token only known by your Next.js app. This secret will be used to prevent unauthorized revalidation attempts. For example, you can access the route (either manually or with a webhook) with the following URL structure:
If using a Route Handler, you should create a secret token only known by your Next.js app. This secret will be used to prevent unauthorized revalidation attempts. For example, you can access the route (either manually or with a webhook) with the following URL structure:
@@ -231,7 +231,7 @@ If an error is thrown while attempting to revalidate data, the last successfully
...
@@ -231,7 +231,7 @@ If an error is thrown while attempting to revalidate data, the last successfully
To opt out of caching for individual `fetch` requests, you can set the `cache` option in `fetch` to `'no-store'`. This will fetch data dynamically, on every request.
To opt out of caching for individual `fetch` requests, you can set the `cache` option in `fetch` to `'no-store'`. This will fetch data dynamically, on every request.
>-Werecommendusingthe[`server-only`package](/docs/getting-started/react-essentials#keeping-server-only-code-out-of-client-components-poisoning) to make sure server data fetching functions are never used on the client.
>-Werecommendusingthe[`server-only`package](/docs/getting-started/react-essentials#keeping-server-only-code-out-of-client-components-poisoning) to make sure server data fetching functions are never used on the client.
@@ -104,19 +104,17 @@ Since the memoization is not shared across server requests and only applies duri
...
@@ -104,19 +104,17 @@ Since the memoization is not shared across server requests and only applies duri
To opt out of memoization in `fetch` requests, you can pass an `AbortController` `signal` to the request.
To opt out of memoization in `fetch` requests, you can pass an `AbortController` `signal` to the request.
```js
```js filename="app/example.js"
const { signal } = new AbortController()
const { signal } = new AbortController()
fetch(url, { signal })
fetch(url, { signal })
```
```
## Data Cache
## Data Cache
Next.js has a built-in Data Cache that **persists** the result of data fetches across incoming **server requests** and **deployments**. This is possible because Next.js extends the native `fetch` API to allow each request on the server to set its own persistent caching semantics.
Next.js has a built-in Data Cache that **persists** the result of data fetches across incoming **server requests** and **deployments**. This is possible because Next.js extends the native `fetch` API to allow each request on the server to set its own persistent caching semantics. n the browser, the `cache` option of `fetch` indicates how a request will interact with the browser'sHTTPcache,withNext.js,the`cache`optionindicateshowaserver-siderequestwillinteractwiththeserversDataCache.
Bydefault,datarequeststhatuse`fetch`are**cached**.Youcanusethe[`cache`](#fetch-optionscache) and [`next.revalidate`](#fetch-optionsnextrevalidate) options of `fetch` to configure the caching behavior.
Bydefault,datarequeststhatuse`fetch`are**cached**.Youcanusethe[`cache`](#fetch-optionscache) and [`next.revalidate`](#fetch-optionsnextrevalidate) options of `fetch` to configure the caching behavior.
> **Good to know**: In the browser, the `cache` option of `fetch` indicates how a request will interact with the browser'sHTTPcache,withNext.js,the`cache`optionindicateshowaserver-siderequestwillinteractwiththeserversDataCache.
<Image
<Image
alt="Diagram showing how cached and uncached fetch requests interact with the Data Cache. Cached requests are stored in the Data Cache, and memoized, uncached requests are fetched from the data source, not stored in the Data Cache, and memoized."
alt="Diagram showing how cached and uncached fetch requests interact with the Data Cache. Cached requests are stored in the Data Cache, and memoized, uncached requests are fetched from the data source, not stored in the Data Cache, and memoized."
Next.js automatically renders and caches routes at build time. This is an optimization that allows you to serve the cached route instead of rendering on the server for every request, resulting in faster page loads.
> **Related terms**:
> **Related terms**:
>
>
> - You may see the terms **Automatic Static Optimization**, **Static Site Generation**, or **Static Rendering** being used interchangeably to refer to the process of rendering and caching routes of your application at build time.
> You may see the terms **Automatic Static Optimization**, **Static Site Generation**, or **Static Rendering** being used interchangeably to refer to the process of rendering and caching routes of your application at build time.
Next.js automatically renders and caches routes at build time. This is an optimization that allows you to serve the cached route instead of rendering on the server for every request, resulting in faster page loads.
To understand how the Full Route Cache works, it'shelpfultolookathowReacthandlesrendering,andhowNext.jscachestheresult:
To understand how the Full Route Cache works, it'shelpfultolookathowReacthandlesrendering,andhowNext.jscachestheresult:
...
@@ -312,6 +310,11 @@ You can opt out of the Full Route Cache, or in other words, dynamically render c
...
@@ -312,6 +310,11 @@ You can opt out of the Full Route Cache, or in other words, dynamically render c
>ThiscachespecificallyappliestoNext.jsandServerComponents,andisdifferenttothebrowser's [bfcache](https://web.dev/bfcache/), though it has a similar result.
Next.js has an in-memory client-side cache that stores the React Server Component Payload, split by individual route segments, for the duration of a user session. This is called the Router Cache.
Next.js has an in-memory client-side cache that stores the React Server Component Payload, split by individual route segments, for the duration of a user session. This is called the Router Cache.
<Image
<Image
...
@@ -332,13 +335,8 @@ This results in an improved navigation experience for the user:
...
@@ -332,13 +335,8 @@ This results in an improved navigation experience for the user:
> **Difference between the Router Cache and Full Route Cache**:
> **Difference between the Router Cache and Full Route Cache**:
>
>
> The Router Cache temporarily stores the React Server Component Payload in the browser for the duration of a user session, whereas the Full Route Cache persistently stores the React Server Component Payload and HTML on the server across multiple user requests.
> The Router Cache temporarily stores the React Server Component Payload in the browser for the duration of a user session, whereas the Full Route Cache persistently stores the React Server Component Payload and HTML on the server across multiple user requests.
> While the Full Route Cache only caches statically rendered routes, the Router Cache applies to both statically and dynamically rendered routes.
>ThiscachespecificallyappliestoNext.jsandServerComponents,andisdifferenttothebrowser's [bfcache](https://web.dev/bfcache/), though it has a similar result.
### Duration
### Duration
...
@@ -387,14 +385,14 @@ When configuring the different caching mechanisms, it's important to understand
...
@@ -387,14 +385,14 @@ When configuring the different caching mechanisms, it's important to understand
|[`const revalidate`](#segment-config-options) | | Revalidate or Opt out | Revalidate or Opt out | |
|[`const revalidate`](#segment-config-options) | | Revalidate or Opt out | Revalidate or Opt out | |
...
@@ -503,7 +501,7 @@ There are two places you can use `revalidatePath`, depending on what you're tryi
...
@@ -503,7 +501,7 @@ There are two places you can use `revalidatePath`, depending on what you're tryi
See the [`revalidatePath` API reference](/docs/app/api-reference/functions/revalidatePath) for more information.
See the [`revalidatePath` API reference](/docs/app/api-reference/functions/revalidatePath) for more information.
> **`revalidatePath` vs. `router.refresh`**:
> **`revalidatePath`** vs. **`router.refresh`**:
>
>
> Calling `router.refresh` will clear the Router cache, and re-render route segments on the server without invalidating the Data Cache or the Full Route Cache.
> Calling `router.refresh` will clear the Router cache, and re-render route segments on the server without invalidating the Data Cache or the Full Route Cache.