Skip to content

Update caching.mdx #81129

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

Merged
merged 2 commits into from
Jul 2, 2025
Merged

Update caching.mdx #81129

merged 2 commits into from
Jul 2, 2025

Conversation

maral
Copy link
Contributor

@maral maral commented Jul 1, 2025

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:

  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.

  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.

fix incorrect information in the docs
@ijjk ijjk added the Documentation Related to Next.js' official documentation. label Jul 1, 2025
@ijjk
Copy link
Member

ijjk commented Jul 1, 2025

Allow CI Workflow Run

  • approve CI run for commit: d7724dc

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

1 similar comment
@ijjk
Copy link
Member

ijjk commented Jul 1, 2025

Allow CI Workflow Run

  • approve CI run for commit: d7724dc

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@icyJoseph
Copy link
Contributor

Hi,

Thanks for the contribution.

That's correct, when the default caching behavior of fetch changed - a few places in the docs got outdated.

@icyJoseph icyJoseph merged commit 5efef45 into vercel:canary Jul 2, 2025
59 checks passed
@maral maral deleted the patch-3 branch July 2, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants