Next.js Discord

Discord Forum

Caching database result in next js

Unanswered
American English Coonhound posted this in #help-forum
Open in Discord
American English CoonhoundOP
guys i think there is a problem i cannot really understand in next js
i know that fetching the data is cached
but what if i get data directly from the mongo db or any data base in the page server component
this is not cached and the worst part is getting metadata then getting the data again in the page !
how can i cache the database query result in the server side

8 Replies

unstable_cache solves this problem beautifully. However it is unstable so use it with caution in production environments
American English CoonhoundOP
so ok i used unstable_cache
but what about exporting my function into a utility instead of server action and use cache from react
American English CoonhoundOP
after reading a bit i realized that cache from react is just memoizing the request and not caching the data (just memo for the life cycle of the request )
Dutch
Im wondering if it makes sense to put user data, like profile picture, username, settings and whatnot, into local storage, and then checking in your data access layer, (or whenever you need it) if its there and only fetching if its not