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

fetch requests not cached between generateMetadata and page component as per docs #74537

Open
steveharrison opened this issue Jan 6, 2025 · 1 comment
Labels
Metadata Related to Next.js' Metadata API.

Comments

@steveharrison
Copy link

Link to the code that reproduces this issue

https://github.com/steveharrison/nextjs-fetch-caching

To Reproduce

  1. Run the demo server: npm run server and npm run start. It will start a server on port 9000.
  2. Run the NextJS project: cd nextjs-project and npm run dev.
  3. Load the NextJS project root page once (probably http://localhost:3000)
  4. Refresh the page

Side note: If you skip step 4, I only get one 200 GET request logged on initial page load, although the Next.js logs say two cache misses, whereas I'd expect one cache hit given only one GET request was received?

 │ GET http://localhost:9000/index.html 200 in 90ms (cache skip)
 │ │ Cache skipped reason: (auto no cache)
 │ GET http://localhost:9000/index.html 200 in 56ms (cache skip)
 │ │ Cache skipped reason: (auto no cache)

Current vs. Expected behavior

Current behaviour: 2 requests get made to the demo server.
Expected behaviour: 1 request gets made to the demo server, as the documentation at https://nextjs.org/docs/app/building-your-application/caching#data-cache says that fetch requests get cached between generateMetadata and page components. I noticed that cache is manually used in this example: #62162, but the docs suggest this is not necessary.

I have disabled strict mode as well, in case duplicates were coming from that.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Nov 15 18:56:27 PST 2024; root:xnu-11215.61.2.501.1~1/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 21.7.1
  npm: 10.5.0
  Yarn: 1.22.22
  pnpm: 9.2.0
Relevant Packages:
  next: 14.2.10
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure, Documentation, Metadata

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@steveharrison steveharrison added the bug Issue was opened via the bug report template. label Jan 6, 2025
@github-actions github-actions bot added Documentation Related to Next.js' official documentation. Metadata Related to Next.js' Metadata API. labels Jan 6, 2025
@cjcheshire
Copy link

I think this may have been around a while. We don't use fetch and have been using a combo of react.cache and unstable_cache to date, but still have 2 requests (it was 4 without it).

I have just explored on canary the use cache directive and have found that does appear to solve the issues we were having however, that's no good for us in production right now

@samcx samcx removed bug Issue was opened via the bug report template. Documentation Related to Next.js' official documentation. labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Metadata Related to Next.js' Metadata API.
Projects
None yet
Development

No branches or pull requests

3 participants