question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSR Example (SSR data is shared)

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:90 (53 by maintainers)

github_iconTop GitHub Comments

8reactions
dai-shicommented, May 28, 2022

v4 has new APIs for context use case. It would be nice to update the example. typing with middleware is improved in v4 too.

5reactions
kirill-konshincommented, Sep 9, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found