Skip to content

Commit 3cef408

Browse files
committed
feat(*) lazy loading of of route /lazy
* split in chunks * had to add a routerStateSelector to the reduxReactRouter store enhancer (to avoid propTypes warnings from redux router) * there's is still a bug remaining, see #12
1 parent a1d7443 commit 3cef408

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

src/containers/Lazy/Lazy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Lazy = ({ children }) => {
44
return (
55
<div>
66
<h2>
7-
This part will be lazy-loaded
7+
This part is lazy-loaded
88
</h2>
99
<div>
1010
{children}

src/containers/LazyHome/LazyHome.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import React from 'react';
33
const LazyHome = () => {
44
return (
55
<div>
6-
Welcome to this part ...
6+
<p>Welcome to component lazy-loading, thanks to <a href="https://github.com/rackt/react-router" title="react-router on github">react-router</a> & <a href="https://webpack.github.io/docs/code-splitting.html">webpack.ensure</a> !</p>
7+
<p>Take a look in your devtools, you'll see some chunks required.</p>
8+
<p>There's a bug remaining on this part (an egde case), please consider contribute to <a href="https://github.com/topheman/react-es6-redux/issues/12">this issue</a> if you've appreciated this project.</p>
79
</div>
810
);
911
};

src/redux/configure-store.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ let combinedCreateStore;
2020
* Higher order function that compose a store creator and returns a new one: (StoreCreator) => (StoreCreator)
2121
*/
2222
const storeEnhancers = [
23-
reduxReactRouter({ createHistory })
23+
reduxReactRouter({
24+
createHistory,
25+
routerStateSelector: state => ({
26+
location: {
27+
pathname: undefined
28+
},
29+
routes: [],
30+
params: {},
31+
components: [],
32+
...state.router
33+
})
34+
})
2435
];
2536

2637
/**

src/routes.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ import {
88
Redux
99
} from './containers/index';
1010

11-
import Lazy from './containers/Lazy/Lazy';
12-
import LazyHome from './containers/LazyHome/LazyHome';
11+
const lazyRouteLoader = (location, cb) => {
12+
require.ensure([], (require) => {
13+
cb(null, require('./containers/Lazy/Lazy'));
14+
});
15+
};
16+
17+
const lazyHomeRouteLoader = (location, cb) => {
18+
require.ensure([], (require) => {
19+
cb(null, require('./containers/LazyHome/LazyHome'));
20+
});
21+
};
1322

1423
export default (
1524
<Route path="/" component={App}>
1625
<IndexRoute component={Home}/>
1726
<Route path="github" component={Github}/>
1827
<Route path="github/user/:username" component={GithubUser}/>
1928
<Route path="redux" component={Redux}/>
20-
<Route path="lazy" component={Lazy}>
21-
<IndexRoute component={LazyHome}/>
29+
<Route path="lazy" getComponent={lazyRouteLoader}>
30+
<IndexRoute getComponent={lazyHomeRouteLoader}/>
2231
</Route>
2332
</Route>
2433
);

webpack.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ var config = {
135135
"css/main": "./src/style/main.scss"
136136
},
137137
output: {
138-
publicPath: "/assets/",
138+
publicPath: "assets/",
139139
filename: "[name]" + hash + ".js",
140+
chunkFilename: 'js/[id]' + hash + '.chunk.js',
140141
path: "./build/assets"
141142
},
142143
cache: true,

0 commit comments

Comments
 (0)