Next.js Discord

Discord Forum

unstable_cache fetches twice every build on Vercel

Unanswered
Satin posted this in #help-forum
Open in Discord
SatinOP
I posted this on Discussions but no one answered this so hopefully someone here could help me.
https://github.com/vercel/next.js/discussions/60704

unstable_cache is supposed to act the same as Data Cache but it's updated every build on Vercel. Tried the same function that is wrapped in it with a route handler won't be updated unless manually revalidated. Is this intended?

PS: my problem is that unstable_cache won't be cached across deployments and it fetches twice always

20 Replies

im not sure what your issue is, but there are 2 things i will add:
1. unstable_cache doesn't seem to dedup yet, so if your running it a tthe same time, you may need to wrap it in react cache
2. the fetch calls the db does became forced to be uncached (as it can be bad if not manualy done), but that doesn't mean the unstable_cache isn't caching
@riský im not sure what your issue is, but there are 2 things i will add: 1. unstable_cache doesn't seem to dedup yet, so if your running it a tthe same time, you may need to wrap it in react cache 2. the `fetch` calls the db does became forced to be uncached (as it can be bad if not manualy done), but that doesn't mean the unstable_cache isn't caching
SatinOP
My question is why the data cached by unstable_cache won't be cached across deployments while it is supposed to act as Data Cache. I expect the data to be stored persistently unless manually revalidated by revalidateTag etc.
oh that is intresting because i too thought it was an independent cache from deployment (but if you change the function then ofc it will change)
after all as you said, its in the data cache
SatinOP
Yeah, and I tried the function I made with a route handler so that it works as Data Cache. It won't be updated across deployments so I believe the function isn't the problem.

Oh, and being fetched TWICE is another problem, sorry I should've been more clear
So my problem is that unstable_cache won't be cached across deployments and it fetches twice always
wait the function gets ran twice 😭
@Satin So my problem is that `unstable_cache` won't be cached across deployments and it fetches twice always
so after the initial request, does it still fetch many times, or is it only the first that somehow does twice
SatinOP
the same one. I've observed PlanetScale executed the same query twice
BUT it won't happen in a route handler meaning the query is done once
and to be sure, is this a vercel issue or nextjs (ie does local dev do same thing)
SatinOP
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). so is this to be a Vercel's issue for not fully supporting unstable_cache?
intresting
if you make the page dynamic export const revalidate = 0, can you see how well it caches
maybe the build is just weird, so im wondering if its more sane if not using it then
or maybe the build is doing something weird with the page, and maybe wrapping the unstable cache in react cache, might make it once
SatinOP
It's weird. With export const revalidate = 0, tried rm -rf .next && next build && next start it only fetched once
hmm
SatinOP
I made this an issue here so hopefully someone could solve the mystery.
https://github.com/vercel/next.js/issues/61423
SatinOP
Seems this is not just about unstable_cache. So the main concern is now simply why fetching data twice on build.