diff --git a/src/client.js b/src/client.js index c2a99a7cd..5ee4ca796 100644 --- a/src/client.js +++ b/src/client.js @@ -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'; @@ -36,38 +36,41 @@ function initSocket() { global.socket = initSocket(); -const component = ( - - !item.deferred} /> - } history={history}> - {getRoutes(store)} - -); - -ReactDOM.render( - - {component} - , - 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 }, () => { + const component = ( + + !item.deferred} /> + } history={history}> + {routes} + + ); -if (__DEVTOOLS__ && !window.devToolsExtension) { - const DevTools = require('./containers/DevTools/DevTools'); ReactDOM.render( -
- {component} - -
+ {component}
, 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( + +
+ {component} + +
+
, + dest + ); + } +}); diff --git a/src/routes.js b/src/routes.js index 63527745d..e193e4ffe 100644 --- a/src/routes.js +++ b/src/routes.js @@ -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) => { @@ -30,29 +17,92 @@ export default (store) => { checkAuth(); } }; - /** * Please keep routes in alphabetical order */ - return ( - - { /* Home (main) route */ } - + if (typeof require.ensure !== 'function') require.ensure = (deps, cb) => cb(require); - { /* Routes requiring login */ } - - - - + return { + path: '/', + component: require('./containers/App/App'), + indexRoute: { + component: require('./containers/Home/Home') + }, + childRoutes: [{ + path: 'login', + getComponent(nextState, cb) { + console.time('gettingComponent'); + 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 */ } - - - - + }, { + 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 */ } - - - ); + }, { + 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'); + } + }] + }; };