redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Problem with loading behaviour when doing SSR and PersistGate

Open rajatbarman opened this issue 5 years ago • 9 comments

Hey guys,

Great solve for the persistence problem, although I have an issue.

<PersistGate loading="loading" persistor={persistor}>

    <MyEntireApp />

</PersistGate/>

When the app is rendered from the server, the react component tree is already created and the view is already rendered, but because of <PersistGate /> the whole tree is reconstructed as it has to render the loading text instead of the whole app while persist is syncing with the store and then again construct the whole app's component tree, which leads to a break in user experience.

Imagine this - Web app Rendered (from server) -> Loading Text Rendered (when client JS comes into play and persistor starts hydrating) -> Web app Re-rendered (after persistor is done hydrating the store)

A not so user friendly experience.

Basically this line in PersistGate creates problem when doing SSR - this.state.bootstrapped ? this.props.children : this.props.loading

react v16 redux-persist v5.7.0

rajatbarman avatar Mar 18 '19 20:03 rajatbarman

To those looking for solution. Don't use <PersistGate /> wrapper, instead wrap this logic over ReactDOM.hydrate

persistor.subscribe(() => {
          /* Hydrate React components when persistor has synced with redux store */
          const { bootstrapped } = persistor.getState();

          if (bootstrapped) {
              ReactDOM.hydrate(
                  <MyEntireApp />,
                  document.getElementById("appOrWhatever")
            );
          }
        });

rajatbarman avatar Mar 18 '19 20:03 rajatbarman

Hi there. Thanks for your answer. Didn't you have an Error similar to: "Warning: Did not expect server HTML to contain a

in
."

I guess that because the Dom Tree is different without the PersistGate you get the Error while rehydrating.

pwnreza avatar May 16 '19 15:05 pwnreza

Having few issues with the SSR in Gatsby. Such as:

  1. could not load the persisted data correctly
  2. providing google/facebook crawling the Gatsby site Any solution or progress for it?

max8hine avatar May 19 '19 05:05 max8hine

@rajatbarman Your solution worked for me! Thanks!

krunalp1993 avatar Nov 14 '19 09:11 krunalp1993

My solution: https://stackoverflow.com/a/62501513/7292383

MiKatre avatar Jun 21 '20 16:06 MiKatre

I'm using struggling to solve this when integrating redux-persist with Gatsby.

alexpchin avatar Nov 06 '20 14:11 alexpchin

https://github.com/vercel/next.js/issues/8240#issuecomment-647699316 another solution from Next.JS community

ybarakhovskyi avatar Nov 30 '20 18:11 ybarakhovskyi

To those looking for solution. Don't use <PersistGate /> wrapper, instead wrap this logic over ReactDOM.hydrate

persistor.subscribe(() => {
          /* Hydrate React components when persistor has synced with redux store */
          const { bootstrapped } = persistor.getState();

          if (bootstrapped) {
              ReactDOM.hydrate(
                  <MyEntireApp />,
                  document.getElementById("appOrWhatever")
            );
          }
        });

@rajatbarman can you show where you put the code you provided? No clue where to put your snippet.

simplenotezy avatar Nov 28 '21 12:11 simplenotezy

bump

6lyxt avatar Jun 16 '23 10:06 6lyxt