SSR Example (SSR data is shared)
See original GitHub issueI’m using Next.js and have a separate file state.js
that has something like this:
export const useSessionStore = create(combine({
session: {},
}, set => ({
setSession: (session) => set({ session }),
})))
And then calling setSession()
on the server (for example in _app.js
) after a successful login. When reading session
on the server + client, it seems to work fine. So far so good.
The problem is: the session
seems to be saved in Node.js memory and shared with everyone(?). When you login on one browser, the same session data is returned when you load the page on another browser. So I’m assuming when I set session on the server, it persists on Node.js side and returns the same session for all clients.
What am I doing wrong, or is zustand not meant for this?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:90 (53 by maintainers)
Top Results From Across the Web
SSR | TanStack Query Docs
SSR. React Query supports two ways of prefetching data on the server and ... This ensures that data is not shared between different...
Read more >NextJS / React SSR: 21 Universal Data Fetching Patterns ...
21 Universal Data Fetching Patterns & Best Practices for NextJS / React SSR.
Read more >Server-Side Rendering (SSR) - Vue.js
If we mutate the shared singleton state with data specific to one user, it can be accidentally leaked to a request from another...
Read more >Bridging the Gap Between SSR and Client-State Persisted ...
A disadvantage of SSR is that the server builds a page for every request from the client.
Read more >Server Side Rendering (SSR) vs. Client Side Rendering (CSR ...
Server Side Rendering (SSR). In Server Side Rendering (SSR) the HTML components of the webpage are generated on the server-side. When a browser...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
v4 has new APIs for context use case. It would be nice to update the example. typing with middleware is improved in v4 too.
next-redux-wrapper
does lots of things behind the scenes. It wraps Next.js hook methods and properly hydrates the state between client and server in various navigation scenarios and hook combinations. Unfortunately, I’m not familiar with Zustand, so I doubt I can help here.