2018-02-21 22:42:41 +01:00
|
|
|
import React from 'react';
|
2018-02-21 17:05:41 +01:00
|
|
|
import { renderToString } from 'react-dom/server';
|
|
|
|
import { createStore } from 'redux';
|
2018-02-21 22:42:41 +01:00
|
|
|
import { Provider } from 'react-redux';
|
2018-02-22 19:48:46 +01:00
|
|
|
import { StaticRouter } from 'react-router-dom';
|
2018-05-23 10:18:50 +02:00
|
|
|
import Reducers from 'client/build/reducers';
|
|
|
|
import GAListener from 'client/build/components/GAListener';
|
|
|
|
import App from 'client/build/app.js';
|
2018-04-18 21:11:25 +02:00
|
|
|
import renderFullPage from '../renderFullPage.js';
|
2018-02-23 20:00:46 +01:00
|
|
|
import Helmet from 'react-helmet';
|
2018-02-21 17:05:41 +01:00
|
|
|
|
2018-04-18 21:01:26 +02:00
|
|
|
const siteConfig = require('../../../config/siteConfig.js');
|
2018-03-31 20:59:37 +02:00
|
|
|
|
2018-02-21 17:05:41 +01:00
|
|
|
module.exports = (req, res) => {
|
|
|
|
let context = {};
|
|
|
|
|
2018-03-31 20:59:37 +02:00
|
|
|
// customize the reducer by passing in intial state configs
|
2018-04-01 03:27:24 +02:00
|
|
|
const MyReducers = Reducers(siteConfig);
|
2018-05-13 20:47:57 +02:00
|
|
|
const MyApp = App;
|
2018-04-01 03:27:24 +02:00
|
|
|
const MyGAListener = GAListener(siteConfig);
|
2018-03-31 20:59:37 +02:00
|
|
|
|
2018-02-21 17:05:41 +01:00
|
|
|
// create a new Redux store instance
|
2018-04-01 03:27:24 +02:00
|
|
|
const store = createStore(MyReducers);
|
2018-02-21 17:05:41 +01:00
|
|
|
|
|
|
|
// render component to a string
|
|
|
|
const html = renderToString(
|
|
|
|
<Provider store={store}>
|
|
|
|
<StaticRouter location={req.url} context={context}>
|
2018-04-01 03:27:24 +02:00
|
|
|
<MyGAListener>
|
|
|
|
<MyApp />
|
|
|
|
</MyGAListener>
|
2018-02-21 17:05:41 +01:00
|
|
|
</StaticRouter>
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
|
2018-02-23 20:00:46 +01:00
|
|
|
// get head tags from helmet
|
|
|
|
const helmet = Helmet.renderStatic();
|
|
|
|
|
2018-02-22 02:34:29 +01:00
|
|
|
// check for a redirect
|
|
|
|
if (context.url) {
|
|
|
|
// Somewhere a `<Redirect>` was rendered
|
2018-02-22 19:48:46 +01:00
|
|
|
return res.redirect(301, context.url);
|
2018-02-22 02:34:29 +01:00
|
|
|
} else {
|
|
|
|
// we're good, send the response
|
|
|
|
}
|
|
|
|
|
2018-02-21 17:05:41 +01:00
|
|
|
// get the initial state from our Redux store
|
|
|
|
const preloadedState = store.getState();
|
|
|
|
|
|
|
|
// send the rendered page back to the client
|
2018-02-23 20:00:46 +01:00
|
|
|
res.send(renderFullPage(helmet, html, preloadedState));
|
2018-02-21 17:05:41 +01:00
|
|
|
};
|