singleton pattern with global in RSC is not supporting?
Unanswered
Asian black bear posted this in #help-forum
Asian black bearOP
While using the redis client as a singleton pattern to check the session in the rsc layout, the connection continuously occurred, so I checked it.
in those test code, why globalForTest.test values resetted to undefined?
is it bug? or am i doing something wrong?
in those test code, why globalForTest.test values resetted to undefined?
is it bug? or am i doing something wrong?
22 Replies
Asian black bearOP
ok when i check it's call seq, testSingleton called twice.
why it called again after rendered? 🤔
(0 => 1): testSingleton => (2): layout => (0 => 1): testSingleton
why it called again after rendered? 🤔
(0 => 1): testSingleton => (2): layout => (0 => 1): testSingleton
how are you doing the test?
Asian black bearOP
just with
next dev + app dir
next dev + app dir
and are those just froma single request or multiple requests
Asian black bearOP
just for every request, testSingleton sequence called twice
can i show you how i set up my singleton?
Asian black bearOP
sure! i'll test again with that method
this log for single request
I called it everywhere on the page so it should show up more than 2, but here it only show up once or twice
ideally every request should show up once only
but it might be inconsistencies with dev server. one should try in prod
Asian black bearOP
Is it a correct for notion client to be created on every request?
Im deploying it on Vercel so it has to be stateless. yes
if you deploy it on non-serverless architecture, i believe you could just make global exports normally lol
i.e nothing can persists between requests
Asian black bearOP
even is it with non-serverless?
Vercel is serverless so no, that doesn't apply
Asian black bearOP
In prod it works as expected.
In dev, keep got "The server is running out of memory, restarting to free up memory", it's why i dig this stuff.
maybe this is causing a memory leak in the dev. (Importing twice in rsc?)
I will debug heap and check second import is disposed. if not i will write a gh issue for it.
thank you!
In dev, keep got "The server is running out of memory, restarting to free up memory", it's why i dig this stuff.
maybe this is causing a memory leak in the dev. (Importing twice in rsc?)
I will debug heap and check second import is disposed. if not i will write a gh issue for it.
thank you!
No problem!
Asian black bearOP
do you know why it behaves like that?
I'd like to know if this is the intended implementation.
I'd like to know if this is the intended implementation.