Bamboozled by some 'use server' code behavior not updating global variable
Unanswered
Schweizerischer Niederlaufhund posted this in #help-forum
Schweizerischer NiederlaufhundOP
I'm new to next.js and I've read the data caching section and searched this forum but I'm still completely stumped.
I have a library with the 'use server' directive at the top. I have two functions that Set and Clear a global string. The string is empty to start. When Set is called, it checks for an empty string, and if empty, it gets some user metadata in auth0 (perhaps with a behind the scenes fetch) and saves it in a global. The Clear simply resets the global to an empty string.
When my code runs, I see the Clear being run, and before Clear exits I print the string and it is empty. But at the top of the next Set, it still has the old value, and my check for empty is false, so its just returned. Its like it can only be set once...
I understand that the behind the scenes auth0 fetch may be cached, but is my global being cached so that even this server side code can't Clear it?
What's happening here, thanks so much!
I have a library with the 'use server' directive at the top. I have two functions that Set and Clear a global string. The string is empty to start. When Set is called, it checks for an empty string, and if empty, it gets some user metadata in auth0 (perhaps with a behind the scenes fetch) and saves it in a global. The Clear simply resets the global to an empty string.
When my code runs, I see the Clear being run, and before Clear exits I print the string and it is empty. But at the top of the next Set, it still has the old value, and my check for empty is false, so its just returned. Its like it can only be set once...
I understand that the behind the scenes auth0 fetch may be cached, but is my global being cached so that even this server side code can't Clear it?
What's happening here, thanks so much!
'use server'
let userId = "";
async function getUser() {
if (userId === "") {
// get data using a 3rd party api (behind the scenes fetch)
const session = await getSession();
userId = session?.user.user_metadata.practitionerId;
console.info("Retrieved userid ", userId);
}
else
console.info("Re-using userid ", userId);
return userId;
}
async function clearUser() {
userId = "";
console.info("user Id is now cleared: ",userId);
}10 Replies
Provide a code
@Anay-208 Provide a code
Schweizerischer NiederlaufhundOP
Updated
@ᴉuɐpɹɐɐ using global variable is very dangerous as it could bleed to other user...
Schweizerischer NiederlaufhundOP
Thanks for your comment. I will use cookies. Yes of course you are right, I was so wrapped up in solving this weird problem I didn't think this through.
But aside from that... any idea why the runtime behavior of next.js be like that? It makes me lose confidence. In my case I'm running the server locally and I'm the only user context running... I completely don't understand how it can have that behavior, which looks like this sequence in practice:
1st call to getUser - user is retrieved and set
clearUser - log shows its ""
2nd call to getUser - log shows userId is being re-used (ie. is already non-empty)
(userId isn't exposed anywhere else. )
But aside from that... any idea why the runtime behavior of next.js be like that? It makes me lose confidence. In my case I'm running the server locally and I'm the only user context running... I completely don't understand how it can have that behavior, which looks like this sequence in practice:
1st call to getUser - user is retrieved and set
clearUser - log shows its ""
2nd call to getUser - log shows userId is being re-used (ie. is already non-empty)
(userId isn't exposed anywhere else. )
its good for in-memory caching purposes but the worst for storing per-request information...
1st call to getUser - user is retrieved and setThats an odd behavior
clearUser - log shows its ""
2nd call to getUser - log shows userId is being re-used (ie. is already non-empty)
but i usually store them in objects
const temp = {
userId: string
}so you access them by reference and not by value