unstable_cache deduping
Unanswered
Argentine ant posted this in #help-forum
Argentine antOP
I was surprised to learn after reading this https://alfonsusardani.notion.site/unstable_cache-from-next-cache-f300b3184d6a472ea5282543d50b9f02 that unstable_cache does not dedupe api calls like fetch does. This is also seemingly confirmed by this page https://nextjs.org/docs/app/building-your-application/caching showing that react cache is not hit by unstable cache. Is there a reason it was built not to use react cache/dedupe requests?
32 Replies
@Argentine ant I was surprised to learn after reading this https://alfonsusardani.notion.site/unstable_cache-from-next-cache-f300b3184d6a472ea5282543d50b9f02 that unstable_cache does not dedupe api calls like fetch does. This is also seemingly confirmed by this page https://nextjs.org/docs/app/building-your-application/caching showing that react cache is not hit by unstable cache. Is there a reason it was built not to use react cache/dedupe requests?
Is there a reason it was built not to use react cache/dedupe requests?Probably because its still in beta/preview. Im not sure though, but theres a reason its still
unstable.where did you find that article btw?
@ᴉuɐpɹɐɐ where did you find that article btw?
Argentine antOP
Perplexity led me to it after searching for more info on unstable_cache
@ᴉuɐpɹɐɐ > Is there a reason it was built not to use react cache/dedupe requests?
Probably because its still in beta/preview. Im not sure though, but theres a reason its still `unstable`.
Argentine antOP
so you think deduping will be added?
after recent events with people protesting that
fetch shouldnt be patched with cache,i think they will move to
unstable_cache being the source of memoization between requeststherefore it has to be as good as
fetchthat is what i think, i am no way in touch with the next.js team so its just a long shot
for SSG and ISR, fetch is better
for SSR ( a.k.a dynamic, per-request rendering ),
for SSR ( a.k.a dynamic, per-request rendering ),
unstable_cache wrapped with cache is better ( if you can't use fetch )Argentine antOP
seems silly we would have to wrap unstable_cache with cache
thats why my library does essentially. its silly but it works
Argentine antOP
https://github.com/vercel/next.js/discussions/65405 this makes it sound like next is going to keep the patched fetch
i see
if unstable_cache were to be next's alternative to fetching 3rd party data without fetch, then it still have to be as good as cache
because unstable_cache is so broken in static site generation 

@ᴉuɐpɹɐɐ thats why my library does essentially. its silly but it works
Argentine antOP
wait you made the nextjs-better-unstable-cache? 😆
yeah, great name isnt it? (sarcasm)
i might come back to that and make it even better
i have some idea on how to make it even better
Argentine antOP
how about just making a pr to fix unstable_cache
good idea, time to familiarize myself in contributing in open source
im just not sure where to start
it has a lot of potential because it's like a built-in tanstack query
yeah, thats where the idea came from
welp i guess i know what ill do over the weekend
Scottish Fold
I think that this info can be outdated, have you tried it after reading the blog? I tried unstable_cache with and without wrappin. In a single request it only fires once when accessed by multiple RSC.
In fact the next docs page you linked say: “Whether the data is cached or uncached, the requests are always memoized to avoid making duplicate requests for the same data during a React render pass.”
@Scottish Fold I think that this info can be outdated, have you tried it after reading the blog? I tried unstable_cache with and without wrappin. In a single request it only fires once when accessed by multiple RSC.
have you tried using it in generateMetadata and server components at the same time?
if so then they have made unstable_cache less unstable 

@Scottish Fold In fact the next docs page you linked say: “Whether the data is cached or uncached, the requests are always memoized to avoid making duplicate requests for the same data during a React render pass.”
If you see in that section there is not mention of unstable_cache in the previous paragraph, indicating that this is talking about
Its use in SSG is still broken, and they are not memoized if used within generateMetadata last time i checked (few weeks ago)
fetch(). I agree that this is what they ultimately want for unstable_cache too but as for now, if you can see the table sent by OP, unstable_cache is not memoized completely.Its use in SSG is still broken, and they are not memoized if used within generateMetadata last time i checked (few weeks ago)
Northern Goshawk
I found out via the same article that unstable_cache doesn't dedupe database calls. I came here trying to figure out if the pattern of wrapping unstable_cache with reacts cache function is a good idea only to find the author of the article here as well😄 I might just go with that solution myself