Next.js Discord

Discord Forum

Caching in nextjs/vercel serverless functions

Unanswered
Forest bachac posted this in #help-forum
Open in Discord
Avatar
Forest bachacOP
Locally, I can get away with just doing:

let sessionCache: SessionCache = {};

Problem: In prod/vercel deployments, after a few more requests are sent to the backend/serverless function, the cache clears.

Question: How can I make the cached variable indefinite, so it doesn't disappear so quickly/at all?

15 Replies

Avatar
DirtyCajunRice | AppDir
maybe you are confused but you cant “cache” a database connection. Thats not how that works.
if the socket is closed, its closed.
you cache data. whether that is in the form of a page, an api response, etc
Avatar
Forest bachacOP
this is an array of cookies not the db connection
Avatar
DirtyCajunRice | AppDir
cookies are, by nature, dynamic to the call. Explain what you are actually trying to do
Avatar
Forest bachacOP
I want to save the auth cookie's value to the server on login in order to check the validity of the auth cookie in future requests
Image
^
Avatar
DirtyCajunRice | AppDir
serverless has no concept of that. If you want non-client cookies, you need sessions, which require a database
Avatar
Forest bachacOP
how does this sort of thing work?

https://github.com/vvo/iron-session

its a nextjs session package that doesnt use a database
Avatar
DirtyCajunRice | AppDir
it says it right there in the description
it does it in cookies
its not storing them. thats why its “stateless”
Avatar
Forest bachacOP
isnt a major drawback of that not being able to invalidate specific cookies?
Avatar
DirtyCajunRice | AppDir
if you want server-side management features of authentication state, you need database. Its not a “drawback” or a “benefit” its a “product-fit” decision
Avatar
Forest bachacOP
k