Caching in nextjs/vercel serverless functions
Unanswered
Forest bachac posted this in #help-forum
Forest bachacOP
Locally, I can get away with just doing:
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?
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
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
Forest bachacOP
this is an array of cookies not the db connection
cookies are, by nature, dynamic to the call. Explain what you are actually trying to do
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
^
serverless has no concept of that. If you want non-client cookies, you need sessions, which require a database
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
https://github.com/vvo/iron-session
its a nextjs session package that doesnt use a database
it says it right there in the description
it does it in cookies
its not storing them. thats why its “statelessâ€
Forest bachacOP
isnt a major drawback of that not being able to invalidate specific cookies?
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
Forest bachacOP
k