vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.88k stars 26.97k forks source link

unstable_cache fetches twice every build on Vercel #61423

Open roxizhauk opened 9 months ago

roxizhauk commented 9 months ago

Link to the code that reproduces this issue

https://github.com/roxizhauk/test-unstable-cache

To Reproduce

  1. git push to deploy on Vercel
  2. The query in getWords() is executed twice on PlanetScale every build

Current vs. Expected behavior

The data cached by unstable_cache is updated every build on Vercel, querying to the database on PlanetScale TWICE.

This getWords() is used in a static page ("/").

Screenshot 2024-01-16 at 7 58 16 PM

The following log shows how the fetch works on development.

Screenshot 2024-01-16 at 7 54 03 PM

In local build, it still fetches twice in the initial build but the cache seems not updated in another build (becasue the query wouldn't be fired in PlanetScale). Also, the same function used in a route handler only fetches once every build.

I expect the cache to be persistent across deployments as Data Cache does, and unstable_cache to fetch only once if another request isn't intended.

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.2.0: Wed Nov 15 21:54:10 PST 2023; root:xnu-10002.61.3~2/RELEASE_X86_64
Binaries:
  Node: 18.18.0
  npm: 10.1.0
  Yarn: N/A
  pnpm: 8.15.1
Relevant Packages:
  next: 14.1.0
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

Data fetching (gS(S)P, getInitialProps)

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

Vercel (Deployed)

Additional context

Tested through "14.1.0".

Fredkiss3 commented 8 months ago

Having the same issue but with fetch cache

varzy commented 8 months ago

Same issue.

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

roxizhauk commented 7 months ago

Wrapping the function with React's cache didn't solve the issue nor did disabling Strict Mode.

This seems to happen not just to unstable_cache. I've found similar issues but no solution.

kradyy commented 2 months ago

I also face the same issue as described above and it seems to be evident in the latest version of next (14.2.5), following for updates.

AhmedBaset commented 2 months ago

I don't think the bug is in unstable_cache but tather on build process of next.js

There was a bad implementation of the generating static pages during bulids, renderToReadableStream used to be called twice during the build. which might have caused your problem. Zack addressed this in #67680. You can test the reproduction with version v15.0.0-canary.70 or higher to see if the problem persists

federicodelpiano commented 1 month ago

@AhmedBaset is this fix going to be released for v14?

AhmedBaset commented 1 month ago

is this fix going to be released for v14?

@federicodelpiano

I don't know. Have you tested the issue with v15.0.0-canary.70? If it's fixed you might want to ping @ztanner to ask whether they will release it in a backport release as they do lately