React Router Redux Middleware
Middleware for React + Router + Redux with Server Side Rendering.
Attention, this is an experimental project. It requires basic knowledge of involved frameworks.
Usage:
npm install react-router-redux-middleware --save-dev
We assume that you should already have express
, webpack
and webpack-dev-server
. You will need them.
Server rendering basically takes your application, applies same Babel transformations as Webpack do, then renders it
to a string. Because of that you will also need a babel-cli
since it's the easiest way to run the server with Babel:
npm install babel-cli --save-dev
We assume that you either have .babelrc
file or babel
section in your package.json
.
Alter your package.json
and add to scripts
section:
Now it's time to configure your router.js
:
;;; { return promise;} { return <Router history=history> <Route path='/' getComponent= /> <Route path='*' component=NotFound/> </Router>; }
And the way how you render your app, this hackery is especially required if you use async routes:
;;;; ;; const mountNode = document; // !!!!! PLEASE NOTE ID OF MOUNT NODE !!!!!const store = ; // !!!!! PLEASE NOTE THE NAME OF VARIABLE !!!!! { ; } ;
Here is the sample index.html
that should be a part of webpack build, e.g. emitted to you output path. It could be a
real file or generated by HtmlWebpackPlugin
, but it has to be known by Webpack.
App<!-- !!!!! MUST EXACTLY MATCH THE SERVER CONFIG !!!!! -->
Now let's set up the server.js
, you can add more customizations if needed:
;;;;; // same file as in client side; // same file as in client side;; ; // this may be omitted but then you need to manually teach Node to ignore non-js files const port = processenvPORT || 3000; const options = createRouter: createRouter initialStateKey: '__PRELOADED_STATE__' // !!!!! MUST MATCH THE CLIENT template templatePath: path outputPath: configoutputpath; if processenvNODE_ENV !== 'production' const compiler = ; const middleware = ; configdevServer { app; }; compiler configdevServer; else const app = ; app; app; app; { if err throw err; console;}