Unverified Commit b3e6b5b6 authored by Joseph's avatar Joseph Committed by GitHub

Docs/fetching data deduping (#80806)

Fixes: https://github.com/vercel/next.js/issues/77077
https://github.com/vercel/next.js/issues/80790

- We are missing the deduping step that wraps with React.cache
- Add some more clarify about passing cache, current and future renders

Closes: https://linear.app/vercel/issue/DOC-4806/fetching-dedupe

---------
Co-authored-by: 's avatarDelba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
parent 74b7cc33
......@@ -234,13 +234,17 @@ export default function BlogPage() {
}
```
## Deduplicating requests with `React.cache`
## Deduplicate requests and cache data
Deduplication is the process of _preventing duplicate requests_ for the same resource during a render pass. It allows you to fetch the same data in different components while preventing multiple network requests to your data source.
One way to deduplicate `fetch` requests is with [request memoization](/docs/app/guides/caching#request-memoization). With this mechanism, `fetch` calls using `GET` or `HEAD` with the same URL and options in a single render pass are combined into one request. This happens automatically, and you can [opt out](/docs/app/guides/caching#opting-out) by passing an Abort signal to `fetch`.
If you are using `fetch`, requests can be deduplicated by adding `cache: 'force-cache'`. This means you can safely call the same URL with the same options, and only one request will be made.
Request memoization is scoped to the lifetime of a request.
If you are _not_ using `fetch`, and instead using an ORM or database directly, you can wrap your data fetch with the [React `cache`](https://react.dev/reference/react/cache) function.
You can also deduplicate `fetch` requests by using Next.js’ [Data Cache](/docs/app/guides/caching#data-cache), for example by setting `cache: 'force-cache'` in your `fetch` options.
Data Cache allows sharing data across the current render pass and incoming requests.
If you are _not_ using `fetch`, and instead using an ORM or database directly, you can wrap your data access with the [React `cache`](https://react.dev/reference/react/cache) function.
```tsx filename="app/lib/data.ts" switcher
import { cache } from 'react'
......
......@@ -113,7 +113,7 @@ Since the memoization is not shared across server requests and only applies duri
Memoization only applies to the `GET` method in `fetch` requests, other methods, such as `POST` and `DELETE`, are not memoized. This default behavior is a React optimization and we do not recommend opting out of it.
To manage individual requests, you can use the [`signal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal) property from [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). However, this will not opt requests out of memoization, rather, abort in-flight requests.
To manage individual requests, you can use the [`signal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal) property from [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
```js filename="app/example.js"
const { signal } = new AbortController()
......
......@@ -61,7 +61,7 @@ fetch(`https://...`, { cache: 'force-cache' | 'no-store' })
fetch(`https://...`, { next: { revalidate: false | 0 | number } })
```
Set the cache lifetime of a resource (in seconds).
Set the cache lifetime of a resource (in seconds). [Data Cache](/docs/app/guides/caching#data-cache).
- **`false`** - Cache the resource indefinitely. Semantically equivalent to `revalidate: Infinity`. The HTTP cache may evict older resources over time.
- **`0`** - Prevent the resource from being cached.
......@@ -71,7 +71,6 @@ Set the cache lifetime of a resource (in seconds).
>
> - If an individual `fetch()` request sets a `revalidate` number lower than the [default `revalidate`](/docs/app/api-reference/file-conventions/route-segment-config#revalidate) of a route, the whole route revalidation interval will be decreased.
> - If two fetch requests with the same URL in the same route have different `revalidate` values, the lower value will be used.
> - As a convenience, it is not necessary to set the `cache` option if `revalidate` is set to a number.
> - Conflicting options such as `{ revalidate: 3600, cache: 'no-store' }` will cause an error.
### `options.next.tags`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment