2018-02-22 19:48:46 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { renderToString } from 'react-dom/server';
|
|
|
|
import { createStore, applyMiddleware } from 'redux';
|
|
|
|
import { Provider } from 'react-redux';
|
2018-02-23 00:43:26 +01:00
|
|
|
import { StaticRouter } from 'react-router-dom';
|
2018-04-18 21:11:25 +02:00
|
|
|
import renderFullPage from '../renderFullPage';
|
2018-02-22 19:48:46 +01:00
|
|
|
import createSagaMiddleware from 'redux-saga';
|
2018-02-23 00:43:26 +01:00
|
|
|
import { call } from 'redux-saga/effects';
|
2018-06-07 00:12:07 +02:00
|
|
|
import Helmet from 'react-helmet';
|
|
|
|
|
2018-05-23 20:16:47 +02:00
|
|
|
import Reducers from '@reducers';
|
|
|
|
import GAListener from '@components/GAListener';
|
|
|
|
import App from '@app';
|
|
|
|
import Sagas from '@sagas';
|
|
|
|
import Actions from '@actions';
|
2018-02-23 03:05:00 +01:00
|
|
|
|
2018-02-23 00:43:26 +01:00
|
|
|
const returnSagaWithParams = (saga, params) => {
|
|
|
|
return function * () {
|
|
|
|
yield call(saga, params);
|
|
|
|
};
|
|
|
|
};
|
2018-02-22 19:48:46 +01:00
|
|
|
|
|
|
|
module.exports = (req, res) => {
|
|
|
|
let context = {};
|
|
|
|
|
|
|
|
// create and apply middleware
|
|
|
|
const sagaMiddleware = createSagaMiddleware();
|
|
|
|
const middleware = applyMiddleware(sagaMiddleware);
|
|
|
|
|
|
|
|
// create a new Redux store instance
|
2018-06-06 21:01:11 +02:00
|
|
|
const store = createStore(Reducers, middleware);
|
2018-02-22 19:48:46 +01:00
|
|
|
|
2018-06-14 22:06:19 +02:00
|
|
|
// create an action to handle the given url,
|
|
|
|
// and create a the saga needed to handle that action
|
2018-03-31 09:20:27 +02:00
|
|
|
const action = Actions.onHandleShowPageUri(req.params);
|
|
|
|
const saga = returnSagaWithParams(Sagas.handleShowPageUri, action);
|
2018-02-22 19:48:46 +01:00
|
|
|
|
2018-06-14 22:06:19 +02:00
|
|
|
// run the saga middleware with the saga call
|
2018-02-23 00:43:26 +01:00
|
|
|
sagaMiddleware
|
|
|
|
.run(saga)
|
|
|
|
.done
|
|
|
|
.then(() => {
|
|
|
|
// render component to a string
|
|
|
|
const html = renderToString(
|
|
|
|
<Provider store={store}>
|
|
|
|
<StaticRouter location={req.url} context={context}>
|
2018-06-06 21:01:11 +02:00
|
|
|
<GAListener>
|
|
|
|
<App />
|
|
|
|
</GAListener>
|
2018-02-23 00:43:26 +01:00
|
|
|
</StaticRouter>
|
|
|
|
</Provider>
|
|
|
|
);
|
2018-02-22 19:48:46 +01:00
|
|
|
|
2018-02-23 03:05:00 +01:00
|
|
|
// get head tags from helmet
|
|
|
|
const helmet = Helmet.renderStatic();
|
|
|
|
|
2018-02-23 00:43:26 +01:00
|
|
|
// check for a redirect
|
|
|
|
if (context.url) {
|
|
|
|
return res.redirect(301, context.url);
|
|
|
|
}
|
2018-02-22 19:48:46 +01:00
|
|
|
|
2018-02-23 00:43:26 +01:00
|
|
|
// get the initial state from our Redux store
|
|
|
|
const preloadedState = store.getState();
|
2018-02-22 19:48:46 +01:00
|
|
|
|
2018-02-23 00:43:26 +01:00
|
|
|
// send the rendered page back to the client
|
2018-02-23 03:05:00 +01:00
|
|
|
res.send(renderFullPage(helmet, html, preloadedState));
|
2018-02-23 00:43:26 +01:00
|
|
|
});
|
2018-02-22 19:48:46 +01:00
|
|
|
};
|