Created
September 15, 2016 00:59
-
-
Save puppybits/9c49bd67d8a5a76477caa2ec13d3bfc3 to your computer and use it in GitHub Desktop.
Rough minimal bootstrap for React w/ code splitting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ReactDom = require('react-dom'); | |
const { Router } = require('react-router'); | |
const { createHistory } = require('history'), | |
history = createHistory(); | |
const mount = window.document.getElementById('app'); | |
if (!mount){ | |
mount = window.document.createElement("div"); | |
mount.id = "app"; | |
window.document.body.appendChild('mount'); | |
} | |
// HTML 5 routing is supposed in webpack and the basic express server | |
require.ensure([], | |
// lazy require allows webpack to HMR the app | |
require => { | |
let routes = require('routes'); | |
ReactDom.render( | |
<Router history={history}> | |
{routes} | |
</Router>, mount); | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const React = require('react'); | |
const { Router, Route } = require('react-router'); | |
const App = React.createClass({ | |
render(){ | |
let {children} = this.props | |
return ( | |
<div className='app'> | |
{children && children.length > 0 ? | |
'insert tombstone here' : | |
children} | |
</div> | |
); | |
} | |
}); | |
// routes | |
var routes = { | |
path: '/', | |
component: App, | |
childRoutes:[ | |
{ | |
path:"/one", | |
getComponents:(a, cb) => | |
require.ensure([], require => { | |
cb(null, require("pages/PageOne")); | |
}) | |
}, | |
{ | |
path:"/two", | |
getComponents:(a, cb) => | |
require.ensure([], require => { | |
cb(null, require("pages/PageTwo")); | |
}) | |
}, | |
] | |
}; | |
module.exports = routes; |
Do i have to remove all these import statements out of both of these files and change them to require? And can the require.ensure be used in my Declarative way of getRoutes
import * as reducers from 'redux/modules' /* <-- this is probably pulling in a lot more imports */
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider' /* <-- this could be getting some or all of material-ui, definitely remove it */
import { MainContainer, HomeContainer, FeedContainer } from 'containers' /* <-- these should all be require.ensure, inside the getComponents of the route */
import {Error404} from 'components' /* <-- this should be require.ensure, inside the getComponents of the route */
Unless you really have to have redux manage your routes I would remove all that setup. It's forcing a ton of libraries to need to be loaded. That should get an app shell up much quicker.
After you have a fast shell coming up you can add things one at a time to see what the impact is on load times.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the moment my index.js looks like this:
And my routes like this: