singleton pattern with global in RSC is not supporting?
Unanswered
Asian black bear posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
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?
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143116183940370522/image.png)
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143116184259149924/image.png)
22 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
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
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143125900389515364/image.png)
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143125900645388308/image.png)
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143125900850901042/image.png)
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
how are you doing the test?
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
Asian black bearOP
just with
next dev + app dir
next dev + app dir
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
and are those just froma single request or multiple requests
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
Asian black bearOP
just for every request, testSingleton sequence called twice
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
can i show you how i set up my singleton?
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
Asian black bearOP
sure! i'll test again with that method
this log for single request
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
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
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143128595141763142/image.png)
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
Asian black bearOP
Is it a correct for notion client to be created on every request?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
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
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
Asian black bearOP
even is it with non-serverless?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
Vercel is serverless so no, that doesn't apply
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
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!
![Image](https://cdn.discordapp.com/attachments/1143116183739052112/1143132472813572096/image.png)
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
No problem!
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
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.