Skip to content

Instantly share code, notes, and snippets.

@puppybits
Created September 15, 2016 00:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puppybits/9c49bd67d8a5a76477caa2ec13d3bfc3 to your computer and use it in GitHub Desktop.
Save puppybits/9c49bd67d8a5a76477caa2ec13d3bfc3 to your computer and use it in GitHub Desktop.
Rough minimal bootstrap for React w/ code splitting
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);
}
);
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;
@puppybits
Copy link
Author

@jasan-s

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