caching during build
Unanswered
thomasschwinn posted this in #help-forum
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.
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
@thomasschwinn 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.
Yeah this is a problem, my own apps are suffering from this one as well (though the impact is negligible for me so I never tried to fix it).
Try the singleton pattern: write the result of the query to
Try the singleton pattern: write the result of the query to
globalThis and for subsequent queries, read from globalThisthx, 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.
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.