Redux-persist: Skip PersistGate while doing server side rendering?

Created on 18 Nov 2017  路  5Comments  路  Source: rt2zz/redux-persist

react is giving html mismatch error, because server is not rendering loading component of persist gate

Most helpful comment

@mark-slepkov: How did you mock out or load the persistor server side? I'm using the localstorage option for redux-persist, so you can't really load the persistor when server side rendering.

All 5 comments

I expect other have more experience with SSR than I, but generally I would recommend not using PersistGate if you are using SSR. redux-persist-cookie-storage might help here, since it allows you have identical state server side and client side during rehydration.

Hey, guys. I solved this problem using code like this.

import {PersistGate as PersistGateClient} from 'redux-persist/integration/react';
class PersistGateServer extends React.Component {
    render() {
        return this.props.children
    }
}

class App extends React.Component {
    render() {
        let runtime = process.env.RUNTIME;
        let PersistGate = PersistGateServer;
        if (runtime === 'browser') {
             PersistGate = PersistGateClient
        }
        return (
                <PersistGate loading={null} persistor={persistor}>
                       <WhateverYouWant />
                </PersistGate>
        )
    }

I hope it is helpful for you.

@mark-slepkov: How did you mock out or load the persistor server side? I'm using the localstorage option for redux-persist, so you can't really load the persistor when server side rendering.

I came across this issue when running my own SSR React application.
I'm currently seeing no issues just stubbing in PersistGate with no props defined:

// client.ts
const appLayout = createElement(ApplicationLayout);
const withRouter = createElement(BrowserRouter, undefined, appLayout);
const withRedux = createElement(ReduxProvider, {store}, withRouter);
const withPersist = createElement(PersistGate, {loading: null, persistor}, withRedux);
const withHelmet = createElement(HelmetProvider, undefined, withPersist);

// server.ts
const appLayout = createElement(ApplicationLayout);
const withRouter = createElement(StaticRouter, { context: {}, location: req.url }, appLayout);
const withRedux = createElement(Provider, { store }, withRouter);
const withPlaceholderPersistor = createElement(PersistGate, null, withRedux); // no persistor! just for structure
const withHelmet = createElement(HelmetProvider, { context: helmetContext }, withPlaceholderPersistor);

this is my solut谋on. 谋t works for me but i am not sure 谋t 谋s correct
莽枚z眉m

Was this page helpful?
0 / 5 - 0 ratings