Next.js Discord

Discord Forum

Data Caching for Authenticated Users

Unanswered
Persian posted this in #help-forum
Open in Discord
PersianOP
When I have authenticated a user, a cookie is set with a JWT, but it only contains the ID.
If I now want to have the name of the person, it is imperfect to make a new request to the database every time the refresh the page for example.
Can I cache the user's data somewhere?

15 Replies

currently the best way of cross request caching from DB is unstable_cache
PersianOP
What is meant by this?
there isn't any method of caching in nextjs that is stable
they have made unstable_cache which caches the return ... (as they see the need)
PersianOP
Isn't that exactly a reason for companies not to use NextJS? So I mean the performance drops massively, if you make a database access every time
as i said, that is the current way 😭 (i hope it becomes stable soon)
PersianOP
But I should not store anything in the session token? I think thats not the best practice? Or what do you think?
iirc nextauth stores it in jwt to reduce requests
PersianOP
yeah, but i tried to create my own auth :)
personally i have found unstable_cache to work well
but i can see the high risks of it
PersianOP
ok, lets see then. thank you for your advice :)
@Persian Isn't that exactly a reason for companies not to use NextJS? So I mean the performance drops massively, if you make a database access every time
Asiatic Lion
Not really, ain’t companies uses in-memory DBs ( like redis, dragonfly, etc… ) for caching rather than that storing it on the same instance
i mean, unstable_cache and fetch cache (at least on vercel), uses some separate instance for the values iirc
Asiatic Lion
And it varies depending on how you deploy your nextjs app, the backend functions could be deployed on edge and each request gets invoked isolated and closed the connection

Unlike when you deploy it to a long running instance