Next.js Discord

Discord Forum

Is it possible to cache the return of a server action until the next build?

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
I have a data source I can barely do 20 requests per day because it's complex and expensive. I want to fetch my whole data source at build time, only once and cache it until the next build process. Would it be possible to do so?
I want to use this cached server action in another endpoint, where I can filter and sort the data without spamming the original data source.
Would unstable_cache solve my issue?
Answered by Asian paper wasp
Or make use of TTL cache and set the cache time to infinity.

So the data are fetched on demand (so that the build time won't get super long just to wait for the data) but is cache forever.

E.g. https://www.npmjs.com/package/@isaacs/ttlcache
View full answer

8 Replies

Toyger
here is couple way to run some pre-build script https://github.com/vercel/next.js/discussions/17479 you can make script to download data from your api and then fetch it from local file instead remote api
Asian paper wasp
Or make use of TTL cache and set the cache time to infinity.

So the data are fetched on demand (so that the build time won't get super long just to wait for the data) but is cache forever.

E.g. https://www.npmjs.com/package/@isaacs/ttlcache
Answer
I will try both a see what works better in my usecase
Can I mark both as solutions?
You can only mark one, so yeah you have to choose the one that is more helpful to you. Don’t worry, people won’t mind if you select someone else’s answer