Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

Client-side error when using code splitting in webpack #128

Closed
pklicnik opened this issue Jan 20, 2016 · 4 comments
Closed

Client-side error when using code splitting in webpack #128

pklicnik opened this issue Jan 20, 2016 · 4 comments

Comments

@pklicnik
Copy link

Our routes file is configured to use require.ensure so we can code-split our page into separate chunks using webpack. Eg:

if (typeof require.ensure === "undefined") {
    require.ensure = require("node-ensure");
}

var Page = require("page.jsx");

module.exports = {
    component: Page,
    path: "/home",
    childRoutes: [
        {
            path: ":id",
            getComponent: function(location, callback) {
                require.ensure([], function() {
                    callback(null, require("home.jsx"));
                });
            }
        }
    ]
};

Server-side rendering succeeds without error, but an error appears in the console when mounted on the client:

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

After debugging through the issue, this seems to be a problem/bug/limitation in react-router.

As a work around, you need to wrap the call to render inside match to pre-load the routes configuration on the client. Snippet of the change needed on the react-engine side

match({ routes: options.routes, location: location }, function() {
  // for any component created by react-router, merge model data with the routerProps
  // NOTE: This may be imposing too large of an opinion?
  var routerComponent = React.createElement(RouterComponent, {
    createElement: function(Component, routerProps) {
      return React.createElement(Component, merge({}, props, routerProps));
    },

    routes: options.routes,
    history: routerHistory.createHistory()
  });

  render(routerComponent, mountNode);
});

Solution is discussed here:
remix-run/react-router#2036 (comment)

Example provided here:
https://github.com/rackt/example-react-router-server-rendering-lazy-routes

@samsel
Copy link
Contributor

samsel commented Jan 20, 2016

@pklicnik is the change needed on the server or client side of react-engine?

pklicnik pushed a commit to pklicnik/react-engine that referenced this issue Jan 20, 2016
@pklicnik
Copy link
Author

@samsel Client side.

See PR #129 for my workaround. Note: I didn't fully test all scenarios, so tweak as needed

pklicnik pushed a commit to pklicnik/react-engine that referenced this issue Jan 20, 2016
commit 650f3d2
Author: Paul Klicnik <[email protected]>
Date:   Wed Jan 20 11:35:36 2016 -0500

    paypal#128: Client-side error when using code splitting in webpack
@dglozic
Copy link

dglozic commented Jan 20, 2016

+1

samsel added a commit that referenced this issue Jan 26, 2016
PR for #128: Client-side error when using code splitting in webpack
@samsel
Copy link
Contributor

samsel commented Jan 26, 2016

merged & published v3.1.0 with the change!

@samsel samsel closed this as completed Jan 26, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants