Next.js Discord

Discord Forum

Build-time data loading

Unanswered
Ichneumonid wasp posted this in #help-forum
Open in Discord
Ichneumonid waspOP
Hello, I have some data on my DB that does not change frequently (if at all), think payment plan details. What would be the best way to load these in my app at build time and have them stored so I dont have to fetch from my DB all the time? Im using a lot of server components so state wont work there. Im using Next.js 16.

1 Reply

Cinnamon Teal
I assume you are using cacheComponents since you mentioned you are using Next.js 16.

If so you can cache that individual function that fetches the payment details (or whatever other data). You can use use cache at the function level to do so. https://nextjs.org/docs/app/api-reference/directives/use-cache#caching-function-output-with-use-cache

This will allow Next.js to use the cached result anytime the app calls getData() (this example function name is from the above doc) and avoid calling the underlying fetch or database call.

Worth checking that whole doc as well as the docs for cacheLife and cacheTag if you want more control over the cache.