spee.ch/server/render/build/handlePageRender.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-04-18 21:01:26 +02:00
"use strict";
var _react = _interopRequireDefault(require("react"));
var _server = require("react-dom/server");
var _redux = require("redux");
var _reactRedux = require("react-redux");
var _reactRouterDom = require("react-router-dom");
2018-09-26 04:43:57 +02:00
var _renderFullPage = _interopRequireDefault(require("../renderFullPage"));
var _reduxSaga = _interopRequireDefault(require("redux-saga"));
var _effects = require("redux-saga/effects");
2018-06-07 00:12:07 +02:00
var _reactHelmet = _interopRequireDefault(require("react-helmet"));
2018-05-23 20:16:47 +02:00
var _reducers = _interopRequireDefault(require("@reducers"));
2018-05-23 10:18:50 +02:00
2018-05-23 20:16:47 +02:00
var _GAListener = _interopRequireDefault(require("@components/GAListener"));
2018-05-23 10:18:50 +02:00
2018-05-23 20:16:47 +02:00
var _app = _interopRequireDefault(require("@app"));
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
var _sagas = _interopRequireDefault(require("@sagas"));
var _actions = _interopRequireDefault(require("@actions"));
2018-04-18 21:01:26 +02:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2018-09-26 04:43:57 +02:00
var returnSagaWithParams = function returnSagaWithParams(saga, params) {
return (
/*#__PURE__*/
regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return (0, _effects.call)(saga, params);
case 2:
case "end":
return _context.stop();
}
}
}, _callee, this);
})
);
};
2018-04-18 21:01:26 +02:00
module.exports = function (req, res) {
2018-09-26 04:43:57 +02:00
var context = {}; // create and apply middleware
var sagaMiddleware = (0, _reduxSaga.default)();
var middleware = (0, _redux.applyMiddleware)(sagaMiddleware); // create a new Redux store instance
var store = (0, _redux.createStore)(_reducers.default, middleware); // create an action to handle the given url,
// and create a the saga needed to handle that action
var action = _actions.default.onHandleShowPageUri(req.params);
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
var saga = returnSagaWithParams(_sagas.default.handleShowPageUri, action); // run the saga middleware with the saga call
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
sagaMiddleware.run(saga).done.then(function () {
// render component to a string
var html = (0, _server.renderToString)(_react.default.createElement(_reactRedux.Provider, {
store: store
}, _react.default.createElement(_reactRouterDom.StaticRouter, {
location: req.url,
context: context
}, _react.default.createElement(_GAListener.default, null, _react.default.createElement(_app.default, null))))); // get head tags from helmet
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
var helmet = _reactHelmet.default.renderStatic(); // check for a redirect
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
if (context.url) {
return res.redirect(301, context.url);
} // get the initial state from our Redux store
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
var preloadedState = store.getState(); // send the rendered page back to the client
2018-04-18 21:01:26 +02:00
2018-09-26 04:43:57 +02:00
res.send((0, _renderFullPage.default)(helmet, html, preloadedState));
});
2018-04-18 21:01:26 +02:00
};