Looking for a working unstable_cache solution in Next.js 15 (prod-ready, deduped, shared across app)
Unanswered
Snowshoe posted this in #help-forum
SnowshoeOP
Hey everyone đź‘‹
I’ve been working on caching in my Next.js 15 app for a few days and I’m running into issues I can’t seem to solve.
I'm trying to use unstable_cache to cache API calls , but I keep getting random cache misses and duplicate fetches even when calling the same cached function with the same arguments.
My goal is to:
âś… Use unstable_cache in a way that works across server actions and components
âś… Define the cached function once
âś… Use it from multiple parts of the app
âś… Avoid any duplicate API calls
âś… Make it deduped, stable, and reliable
âś… Follow a production-grade pattern
Right now, I have my cache logic and server actions defined outside the /app folder, and I just realized that might be breaking the cache behavior. I’ve tried wrapping with closures, using factories, even storing cache functions in maps, but I still don’t get consistent results.
The official docs don’t go deep enough into reusable patterns, dynamic inputs, or cache function identity — and I feel stuck.
If anyone has a reliable structure or example repo, I’d really appreciate it. I just want to define a cached function once and use it anywhere without worrying about duplicate fetches or broken deduping. 🙏
Thanks a ton in advance !
I’ve been working on caching in my Next.js 15 app for a few days and I’m running into issues I can’t seem to solve.
I'm trying to use unstable_cache to cache API calls , but I keep getting random cache misses and duplicate fetches even when calling the same cached function with the same arguments.
My goal is to:
âś… Use unstable_cache in a way that works across server actions and components
âś… Define the cached function once
âś… Use it from multiple parts of the app
âś… Avoid any duplicate API calls
âś… Make it deduped, stable, and reliable
âś… Follow a production-grade pattern
Right now, I have my cache logic and server actions defined outside the /app folder, and I just realized that might be breaking the cache behavior. I’ve tried wrapping with closures, using factories, even storing cache functions in maps, but I still don’t get consistent results.
The official docs don’t go deep enough into reusable patterns, dynamic inputs, or cache function identity — and I feel stuck.
If anyone has a reliable structure or example repo, I’d really appreciate it. I just want to define a cached function once and use it anywhere without worrying about duplicate fetches or broken deduping. 🙏
Thanks a ton in advance !
1 Reply
West African Lion
May I ask why you are not using the fetch cache? We had been using unstable_cached functions to fetch data from the APIs, but recently switched to just relying on the fetch cache (for example which revalidate). That works very well for us.
Of course, if you still do expensive modifications to the response data, it might make sense to cache the whole function, but using the fetch cache and unstable_cache seems like an overkill to me now.
When we were using unstable_cache, it worked nicely - we even had a two-level cache layer with a short term and a long term cache. Maybe you have something in your function that disabled the caching mechanism?
Of course, if you still do expensive modifications to the response data, it might make sense to cache the whole function, but using the fetch cache and unstable_cache seems like an overkill to me now.
When we were using unstable_cache, it worked nicely - we even had a two-level cache layer with a short term and a long term cache. Maybe you have something in your function that disabled the caching mechanism?