unstable_cache does not work in build time
Answered
Tomistoma posted this in #help-forum
TomistomaOP
Hi there! Currently migrating from pages to app router. I prerender about 1000 pages, which all have about 10 API requests per page.tsx route.
In the past, I was caching those API requests in build-time with some local file cache script like this: https://flaviocopes.com/nextjs-cache-data-globally/. I thought it would be a good time to migrate off to unstable_cache. Turns out, this does not work in build time. I tried it out and it made tens of thousands of API requests, caching none. (Even more, since we have to load data twice, once in the Page() component and once in the generateMetadata() method. Plus, I have some requests that almost all pages use). Build times went from 4-5min with pages router to 8min with app router (same data, same components etc.)
Is it true that these caching methods don't work in build time? Or am I doing something wrong? And is there another way with Vercel Data Cache to cache function results in build time? Otherwise, I would just continue to use my filesystem cache.
I prepared a little demo which you can deploy too: https://github.com/madebyfabian/nextjs-cache-test (demo https://nextjs-cache-test-kappa.vercel.app/)
It prerenders 100 pages
I also tried fetch-level caching, but it's the same (see below for details)
In the past, I was caching those API requests in build-time with some local file cache script like this: https://flaviocopes.com/nextjs-cache-data-globally/. I thought it would be a good time to migrate off to unstable_cache. Turns out, this does not work in build time. I tried it out and it made tens of thousands of API requests, caching none. (Even more, since we have to load data twice, once in the Page() component and once in the generateMetadata() method. Plus, I have some requests that almost all pages use). Build times went from 4-5min with pages router to 8min with app router (same data, same components etc.)
Is it true that these caching methods don't work in build time? Or am I doing something wrong? And is there another way with Vercel Data Cache to cache function results in build time? Otherwise, I would just continue to use my filesystem cache.
I prepared a little demo which you can deploy too: https://github.com/madebyfabian/nextjs-cache-test (demo https://nextjs-cache-test-kappa.vercel.app/)
It prerenders 100 pages
/posts/[id]
and has some API requests that it uses for every page.I also tried fetch-level caching, but it's the same (see below for details)
Answered by Arinji
Yea this is probs a vercel issue, your gonna have to check in the vercel community
12 Replies
TomistomaOP
Interestingly, building the repo locally works fine with unstable_cache.
TomistomaOP
I just tried using fetch-level caching (see https://github.com/madebyfabian/nextjs-cache-test/tree/fetch-level-caching). I added a proxy that logs if a request is actually being made. When deploying to vercel, you see that it also fetches the same endpoints many times
Cuckoo wasp
I think what I have seen locally it is caching the api call on server not on client side so if you make api call your server it doesn't call the server function again instead it uses what is stored in cache in server for responding. It is not client side caching it is server side caching valid for 1 year
TomistomaOP
@Cuckoo wasp This is about prerendering while the build runs, and not in runtime, so it has nothing to do with caching server/client side
Cuckoo wasp
Okay got it so I don't know what is going worng
Caching works whilst in local build right? @Tomistoma
This might just be a vercel issue
TomistomaOP
@Arinji Yes when building locally with
next build
, the caching works and during the build, all the methods are only executing once, all subsequent calls are being cached.@Tomistoma <@890486507872342027> Yes when building locally with `next build`, the caching works and during the build, all the methods are only executing once, all subsequent calls are being cached.
Yea this is probs a vercel issue, your gonna have to check in the vercel community
Answer
TomistomaOP
@Arinji Alright, thank you!
@Tomistoma <@890486507872342027> Alright, thank you!
no worries, mark a solution for now, if you get a vercel answer feel free to add it here
TomistomaOP
FYI I asked the Vercel Help AI and it also confirms that unstable_cache does not work in build time and suggests to keep the file system cache. So it's not a "bug" but more of a missing feature in Vercel.