React Router has two great features that seem like they might not work well together: server side rendering and code splitting.
This minimal demo shows how to get the benefits of server rendering and partial app loading with lazy routes and webpack's code splitting.
npm install
npm start
open http://localhost:5000
- We export array instead of object to use multi-compiler option.
- There is new
getNodeModules
function to grab all instaled modules (as map). Then we can make them as external to prevent processing by webpack - there is simple
baseConfig
to show how many elements we can mark as defaults. Of course it would be nice to put all those things into separate files like
./webpack/base.config.js
./webpack/server.config.js
./webpack/client.config.js
- New
App.css
file - Simple import s from
./App.css
inApp.js
- Simple import img from
./App.png
inApp.js
- While we don't want to use
babel-node
. We have to swith tonode __build__/server.js
. - There are some new packages for postcss and stuff like that.
- If you build server and client version at once it takes more time (obvious - multiple compilations). But you still (i think) can have hot-reloading and stuff.
server.js
is quite big. I don't know if there are any significant performance drops
- UNIVERSAL LIKE HELL!
- Webpack's
require.ensure
defines code splitting points in the app. - We polyfill
require.ensure
for node to just do a normalrequire
. - The server renders the app with
match
and the stateless<RoutingContext/>
. - When the client JavaScript loads, we use
match
to trigger the split code to load before rendering. If we didn't do that, then the first render would benull
and not reuse the server rendered markup. - We render on the client.
- We raise our arms in the air in triumph.