Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Nov 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

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` will automatically sets the `cache` to `cache: force-cache`. This changed in version 15, to `cache: no-store`
ijjk marked this conversation as resolved.
Show resolved Hide resolved

```tsx filename="app/page.tsx" switcher
import { notFound } from 'next/navigation'
Expand All @@ -275,14 +279,14 @@ interface Post {
}

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()
if (!post) notFound()
return post
}

export async function generateStaticParams() {
let posts = await fetch('https://api.vercel.app/blog').then((res) =>
let posts = await fetch('https://api.vercel.app/blog', { cache: 'force-cache' }).then((res) =>
res.json()
)

Expand Down
Loading