Next.js Discord

Discord Forum

Singleton with SSR

Unanswered
svidskiy posted this in #help-forum
Open in Discord
I'm interested in one very interesting question, why when we create stores (all stores should be singletons) they are not on the server side? Since the whole next application is server-side

9 Replies

@svidskiy I'm interested in one very interesting question, why when we create stores (all stores should be singletons) they are not on the server side? Since the whole next application is server-side
Most stuff are just functions, that runs only for a specific amount of time. So they won’t stay forever there. And that’s why the singletons will be empty after some time
Chub mackerel
I guess it because of two thing. first, Nextjs follows stateless model for server-side render. also Nextjs create new SSR environment for each request, make this model more useful. second is each request in a Next.js server-side application is isolated from other requests to ensure privacy and correctness
@svidskiy So it creates like a different app for each user?
No, each request creates its own function, that will be executed on server. This task runs for the time it’s needed and the rest is handled clientside
@svidskiy So the store is created on the server side and then hits the client via hydration?
States on serverside are very rare. There are only a few like url, cookies, headers, … so when you want to have constant states, use one of them
@B33fb0n3 States on serverside are very rare. There are only a few like url, cookies, headers, … so when you want to have constant states, use one of them
No, the original question was about creating a store and why it remains only on the client side if it is created on the server. And how it's works
@svidskiy No, the original question was about creating a store and why it remains only on the client side if it is created on the server. And how it's works
Welll, you can create serverside variables and pass them to the client to store them there. As said: the server has only some states, that are for specific cases. So one solution would be, to pass your serverside data to the client and store them there. Some examples for the clientside store are useState, useContext, …
@svidskiysolved?