Unverified Commit 817a4a19 authored by Rafael Andeloci's avatar Rafael Andeloci Committed by GitHub

[Docs] updating information about fetch caching in the fetching docs (#72156)

Closes: https://github.com/vercel/next.js/issues/72152

This PR fixes a information about fetching being memoized by default in
the [fetching
docs](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching#reusing-data-across-multiple-functions)

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent bc5443dd
...@@ -263,7 +263,11 @@ This example caches the result of the database query for 1 hour (3600 seconds). ...@@ -263,7 +263,11 @@ This example caches the result of the database query for 1 hour (3600 seconds).
Next.js uses APIs like `generateMetadata` and `generateStaticParams` where you will need to use the same data fetched in the `page`. Next.js uses APIs like `generateMetadata` and `generateStaticParams` where you will need to use the same data fetched in the `page`.
If you are using `fetch`, requests are automatically [memoized](/docs/app/building-your-application/caching#request-memoization). This means you can safely call the same URL with the same options, and only one request will be made. If you are using `fetch`, requests can be [memoized](/docs/app/building-your-application/caching#request-memoization) 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.
> **Good to know:**
>
> - In previous versions of Next.js, using `fetch` would have a default `cache` value of `force-cache`. This changed in version 15, to a default of `cache: no-store`.
```tsx filename="app/page.tsx" switcher ```tsx filename="app/page.tsx" switcher
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
...@@ -275,16 +279,18 @@ interface Post { ...@@ -275,16 +279,18 @@ interface Post {
} }
async function getPost(id: string) { async function getPost(id: string) {
let res = await fetch(`https://api.vercel.app/blog/${id}`) let res = await fetch(`https://api.vercel.app/blog/${id}`, {
cache: 'force-cache',
})
let post: Post = await res.json() let post: Post = await res.json()
if (!post) notFound() if (!post) notFound()
return post return post
} }
export async function generateStaticParams() { export async function generateStaticParams() {
let posts = await fetch('https://api.vercel.app/blog').then((res) => let posts = await fetch('https://api.vercel.app/blog', {
res.json() cache: 'force-cache',
) }).then((res) => res.json())
return posts.map((post: Post) => ({ return posts.map((post: Post) => ({
id: post.id, id: post.id,
......
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