Асинхронные маршруты вызывают ошибку недопустимой контрольной суммы на стороне сервера

Я использую Webpack, react, react-router, react-redux, redux и simple-redux-router.

Я получил эту ошибку при использовании реактивного маршрутизатора с асинхронными маршрутами и рендерингом на стороне сервера:

bundle.js:1 Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:

(client) <noscript data-reacti
(server) <div data-reactid=".1

Мой route.cjsx имеет это:

# Routes
path: 'game'
getComponent: (location, cb) =>
    require.ensure [], (require) =>
        cb null, require './views/game'

Если я изменю его на это, я больше не получаю эту ошибку:

# Routes
path: 'game'
getComponent: (location, cb) =>
    cb null, require './views/game'

Есть ли лучший способ решить эту проблему при использовании асинхронных маршрутов?


person Kevin Ghadyani    schedule 16.02.2016    source источник


Ответы (1)


Я получил это как You're trying to render a component to the document using server rendering but the checksum was invalid. This usually means you rendered a different component type or props on the client from the one on the server, or your render() methods are impure.

И исправил это, используя match на клиенте, как описано здесь: https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md#async-routes

Документ предлагает:

match({ history, routes }, (error, redirectLocation, renderProps) => {
  render(<Router {...renderProps} />, mountNode)
})

Конкретный клиентский код, отличный от JSX, который работает для меня (будет немного реорганизован):

var match = ReactRouter.match;
var Router = React.createFactory(ReactRouter.Router);
var Provider = React.createFactory(ReactRedux.Provider)
match({ history: appHistory, routes: routes }, function (error, redirectLocation, renderProps) {
  ReactDOM.render(
      Provider({store: store},
      Router(renderProps)),
        $(document)[0]
  );
});
person firasd    schedule 31.03.2016