#singleton pattern with global in RSC is not supporting?

23 messages · Page 1 of 1 (latest)

warm karma
#

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?

broken yokeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

warm karma
#

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

lament frigate
#

how are you doing the test?

warm karma
#

just with
next dev + app dir

lament frigate
#

and are those just froma single request or multiple requests

warm karma
#

just for every request, testSingleton sequence called twice

lament frigate
#

can i show you how i set up my singleton?

warm karma
#

sure! i'll test again with that method

lament frigate
#

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

warm karma
#

Is it a correct for notion client to be created on every request?

lament frigate
#

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

lament frigate
warm karma
lament frigate
#

Vercel is serverless so no, that doesn't apply

warm karma
#

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!

lament frigate
#

No problem!