next.js
5efef452 - Update caching.mdx (#81129)

Commit
180 days ago
Update caching.mdx (#81129) ### What? Fix misleading information in the docs about mixing cached and uncached `fetch` calls during dynamic rendering. ### Why? I followed the guide (using Next.js 15.3.2) and tried it out with this code: ```ts const joke = await fetch("https://api.chucknorris.io/jokes/random", { cache: "no-cache", }); const jokeData = await joke.json(); console.log("Fetched joke:", jokeData.value); const joke2 = await fetch( "https://api.chucknorris.io/jokes/random?category=dev" ); const jokeData2 = await joke2.json(); console.log("Fetched joke2:", jokeData2.value); ``` After building and starting the production server, when I refresh the page, both `jokeData.value` and `jokeData2.value` are different, uncached. If I want the other `fetch` to be cached, I need to explicitly enable caching, e.g. ```ts const joke = await fetch("https://api.chucknorris.io/jokes/random", { cache: "no-cache", }); const jokeData = await joke.json(); console.log("Fetched joke:", jokeData.value); const joke2 = await fetch( "https://api.chucknorris.io/jokes/random?category=dev", { cache: "force-cache", } ); const jokeData2 = await joke2.json(); console.log("Fetched joke2:", jokeData2.value); ``` This way the first fetch is not cached while the second one is. ### How? The previous version claimed: > Other `fetch` requests that do not opt out of caching will still be cached in the Data Cache. But below in the guide, we can see this: > The default caching behavior of fetch (e.g., when the cache option is not specified) is equal to setting the cache option to no-store: So the first citation is misleading - the fetch requests are not cached by default (with dynamic rendering - static rendering would cache the single call it into the output), so we actually need to explicitly enable caching to have them cached. Co-authored-by: Joseph <joseph.chamochumbi@vercel.com>
Author
Parents
Loading