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.

Is there a way to hydrate a redux slice with the store's state obtained during SSR?

See original GitHub issue

In the readme, section How It Works, it is mentioned that

[During SSR] connected components may alter the store’s state, but the modified state will not be transferred to the client.

However, I have a redux slice (I’m using redux toolkit) that has the specific role of determining which fonts should be preloaded, which is only useful in the initial HTML. I’d love for the client-side logic to be just “keep whatever state was determined server-side.”

Is there any way to hydrate the state (of a slice) with the SSR’s altered store’s state? I’d love a clean solution, but TBH I’d really appreciate a hacky one too!

Thanks!


PS: I’m asking because the logic necessary to dynamically determine which fonts need to be preloaded based on which components are used in the page requires one “synchronous” dispatch right inside each render function, which the execution time of each component to 5-10ms instead of <1ms on first render.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kirill-konshincommented, Jul 22, 2021

The problem is that there are no lifecycle methods that can get the state after render, only before, in normal Next.js lifecycle hooks.

You should not “redo” the work that’s already done on server, just make sure your server-rendered components are not writing the state, then your client during initial render will get the server state in a proper form. Then you can add client-only components that would do their portion of work.

0reactions
Sheraffcommented, Jul 22, 2021

As far as I understand, this only allows you to create a server-side state from the request itself, not from the render pass. But during SSR, connected components can alter the state.

Basically what I’m going for is to not redo client-side the work that has already been done server-side.

However, one of the reasons I want to avoid redoing the work client slide is because for connected components to alter the redux state during SSR, you need to synchronously dispatch redux actions, which is extremely suboptimal in terms of react perfs (component renders that usually take 0.1ms go up to 10ms) and feels hacky. It feels like I could allow that server-side if the resulting page is cached and the work isn’t repeated client-side. But it’s not a really good idea either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strategies for hydrating a store from SSR #47 - GitHub
When the client hydrates, a new global client-side Redux store is created and preloaded with the serialized state.
Read more >
Usage Guide - Redux Toolkit
It lets you decide how you want to handle everything, like store setup, what your state contains, and how you want to build...
Read more >
How to integrate Redux — with Next.js and SSR
This value is used to HYDRATE the state with the server fetched data — or any other pre-existing data for that matter. The...
Read more >
How to properly hydrate Redux state in NextJS on page refresh?
The basic idea is, you first render the page fully and then update the page using the clients storage information. Directly incorporating the ......
Read more >
Persist state with Redux Persist using Redux Toolkit in React
We'll use it in this tutorial to learn how Redux Persist works. ... slices/userSlice"; export const store = configureStore({ reducer: ...
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