Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(routing): Code splitting, named routes, no SSR mismatch issues #1298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import ReactDOM from 'react-dom';
import createStore from './redux/create';
import ApiClient from './helpers/ApiClient';
import io from 'socket.io-client';
import {Provider} from 'react-redux';
import { Router, browserHistory } from 'react-router';
import { Provider } from 'react-redux';
import { Router, browserHistory, match } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import { ReduxAsyncConnect } from 'redux-async-connect';
import useScroll from 'scroll-behavior/lib/useStandardScroll';
Expand Down Expand Up @@ -36,38 +36,41 @@ function initSocket() {

global.socket = initSocket();

const component = (
<Router render={(props) =>
<ReduxAsyncConnect {...props} helpers={{client}} filter={item => !item.deferred} />
} history={history}>
{getRoutes(store)}
</Router>
);

ReactDOM.render(
<Provider store={store} key="provider">
{component}
</Provider>,
dest
);

if (process.env.NODE_ENV !== 'production') {
window.React = React; // enable debugger

if (!dest || !dest.firstChild || !dest.firstChild.attributes || !dest.firstChild.attributes['data-react-checksum']) {
console.error('Server-side React render was discarded. Make sure that your initial render does not contain any client-side code.');
}
}
const routes = getRoutes(store);
match({ history, routes: routes }, () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match is useless here, no ?

You don't use parameters of callback, can you explain to me why you have put a match?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. match is going to wait until React Router has finished getting components. In other words, React Router will fetch chunks and tell us via match when it's ready to render the entire view.
If we don't wait then React Router will render the view without chunks and that view will be different than what we got from server side rendering. This will cause React to discard SSR content.

const component = (
<Router render={(props) =>
<ReduxAsyncConnect {...props} helpers={{client}} filter={item => !item.deferred} />
} history={history}>
{routes}
</Router>
);

if (__DEVTOOLS__ && !window.devToolsExtension) {
const DevTools = require('./containers/DevTools/DevTools');
ReactDOM.render(
<Provider store={store} key="provider">
<div>
{component}
<DevTools />
</div>
{component}
</Provider>,
dest
);
}

if (process.env.NODE_ENV !== 'production') {
window.React = React; // enable debugger

if (!dest || !dest.firstChild || !dest.firstChild.attributes || !dest.firstChild.attributes['data-react-checksum']) {
console.error('Server-side React render was discarded. Make sure that your initial render does not contain any client-side code.');
}
}

if (__DEVTOOLS__ && !window.devToolsExtension) {
const DevTools = require('./containers/DevTools/DevTools');
ReactDOM.render(
<Provider store={store} key="provider">
<div>
{component}
<DevTools />
</div>
</Provider>,
dest
);
}
});
114 changes: 82 additions & 32 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import React from 'react';
import {IndexRoute, Route} from 'react-router';
import { isLoaded as isAuthLoaded, load as loadAuth } from 'redux/modules/auth';
import {
App,
Chat,
Home,
Widgets,
About,
Login,
LoginSuccess,
Survey,
NotFound,
} from 'containers';

export default (store) => {
const requireLogin = (nextState, replace, cb) => {
Expand All @@ -30,29 +17,92 @@ export default (store) => {
checkAuth();
}
};

/**
* Please keep routes in alphabetical order
*/
return (
<Route path="/" component={App}>
{ /* Home (main) route */ }
<IndexRoute component={Home}/>
if (typeof require.ensure !== 'function') require.ensure = (deps, cb) => cb(require);

{ /* Routes requiring login */ }
<Route onEnter={requireLogin}>
<Route path="chat" component={Chat}/>
<Route path="loginSuccess" component={LoginSuccess}/>
</Route>
return {
path: '/',
component: require('./containers/App/App'),
indexRoute: {
component: require('./containers/Home/Home')
},
childRoutes: [{
path: 'login',
getComponent(nextState, cb) {
console.time('gettingComponent');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave these console.time calls in? :)

store.dispatch({
type: 'WEBPACK_LOAD'
});
require.ensure([], (require) => {
cb(null, require('./containers/Login/Login'));
store.dispatch({
type: 'WEBPACK_LOAD_END'
});
console.timeEnd('gettingComponent');
}, 'login');
}

{ /* Routes */ }
<Route path="about" component={About}/>
<Route path="login" component={Login}/>
<Route path="survey" component={Survey}/>
<Route path="widgets" component={Widgets}/>
}, {
path: 'about',
getComponent(nextState, cb) {
console.time('gettingComponent');
store.dispatch({
type: 'WEBPACK_LOAD'
});
require.ensure([], (require) => {
cb(null, require('./containers/About/About'));
store.dispatch({
type: 'WEBPACK_LOAD_END'
});
console.timeEnd('gettingComponent');
}, 'about');
}

{ /* Catch all route */ }
<Route path="*" component={NotFound} status={404} />
</Route>
);
}, {
path: 'survey',
getComponent(nextState, cb) {
require.ensure([], (require) =>
cb(null, require('./containers/Survey/Survey')), 'survey');
}
}, {
path: 'widgets',
getComponent(nextState, cb) {
store.dispatch({
type: 'WEBPACK_LOAD'
});
require.ensure([], (require) => {
cb(null, require('./containers/Widgets/Widgets'));
store.dispatch({
type: 'WEBPACK_LOAD_END'
});
}, 'widgets');
}
}, {
onEnter: requireLogin,
childRoutes: [
{
path: 'chat',
getComponent(nextState, cb) {
require.ensure([], (require) =>
cb(null, require('./containers/Chat/Chat')), 'chat');
}
},
{
path: 'loginSuccess',
getComponent(nextState, cb) {
require.ensure([], (require) =>
cb(null, require('./containers/LoginSuccess/LoginSuccess')), 'loginsuccess');
}
}
]
}, {
path: '*',
getComponent(nextState, cb) {
require.ensure([], (require) =>
cb(null, require('./containers/NotFound/NotFound')), '404');
}
}]
};
};