Last active
October 12, 2017 20:44
-
-
Save zackargyle/5d91614739a12c5d5e77c5ebfb29de2c to your computer and use it in GitHub Desktop.
Sample of Pinterest Mweb code splitting
This file contains 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
// Create a loader | |
const Closeup = () => import(/* webpackChunkName: "CloseupPage" */ 'app/mobile/routes/CloseupPage'); | |
// Register it to the route | |
route('/pin/:pinId', routes.Closeup, { name: 'Closeup' }), | |
// Render a react-router-v4 Route with the route bundle loader | |
<Route exact key="matched-route" path={path} render={matchProps => | |
<PageRoute | |
bundleLoader={loader} | |
routeName={name} | |
{...matchProps} | |
{...props} | |
/>} | |
/> | |
// Async load the route bundle | |
class PageRoute extends PureComponent { | |
render() { | |
const { bundleLoader, ...props } = this.props; | |
return <Loader loader={bundleLoader} {...props} />; | |
} | |
} | |
// Load it and render | |
class Loader extends PureComponent { | |
componentWillMount() { | |
this.props.loader().then(module => { | |
this.setState({ LoadedComponent: module.default }); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment