Next.js Discord

Discord Forum

caching during build

Unanswered
thomasschwinn posted this in #help-forum
Open in Discord
I have a fetch in my layout, everything works fine with it, apart from during build time the query in this fetch hits the api on every page that’s build. Is there a way to cache the fetch during buildtime so it only queries one time and after that it uses the cached version?

I tried unstable_cache and react cache but these caches only work somehow ‚after‘ build.

Don‘t get me wrong, I‘m familiar with revaliditing and all of these things. I just would like to reduce the queries during build, because I‘m hitting an external api. I would expect during build fetches are cached, but they are not. I can see it in my logs when it hits my own db.

2 Replies

thx, this works somehow, but still during build, it hits the api maybe 20 times.
My solution is now that before the build there is a script fetching from the api, weiting the result into a file and later in runtime it revalidates from the api gain.
It works, but I‘m not so super crazy about this solution. If somebody has a better idea, I‘m open to it.