diff --git a/server/render/build/handleShowRender.js b/server/render/build/handleShowRender.js index 854717dc..11c2c9ee 100644 --- a/server/render/build/handleShowRender.js +++ b/server/render/build/handleShowRender.js @@ -34,6 +34,28 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +var createCanonicalLink = require('../../../utils/createCanonicalLink'); + +var getCanonicalUrlFromShow = function getCanonicalUrlFromShow(show) { + var requestId = show.requestList[show.request.id]; + var requestType = show.request.type; + + switch (requestType) { + case 'ASSET_DETAILS': + return createCanonicalLink({ + asset: show.assetList[requestId.key] + }); + + case 'CHANNEL': + return createCanonicalLink({ + channel: show.channelList[requestId.key] + }); + + default: + return null; + } +}; + var returnSagaWithParams = function returnSagaWithParams(saga, params) { return ( /*#__PURE__*/ @@ -115,6 +137,14 @@ module.exports = function (req, res) { var boundSaga = returnSagaWithParams(saga, boundAction); // run the saga middleware with the saga call sagaMiddleware.run(boundSaga).done.then(function () { + // redirect if request does not use canonical url + var canonicalUrl = getCanonicalUrlFromShow(store.getState().show); + + if (canonicalUrl && canonicalUrl !== req.originalUrl) { + console.log("redirecting ".concat(req.originalUrl, " to ").concat(canonicalUrl)); + res.redirect(canonicalUrl); + } + return renderPage(store); }); } else { diff --git a/server/render/src/handleShowRender.jsx b/server/render/src/handleShowRender.jsx index 721e0f60..f8445283 100644 --- a/server/render/src/handleShowRender.jsx +++ b/server/render/src/handleShowRender.jsx @@ -15,12 +15,26 @@ import App from '@app'; import Sagas from '@sagas'; import Actions from '@actions'; +const createCanonicalLink = require('../../../utils/createCanonicalLink'); + +const getCanonicalUrlFromShow = show => { + const requestId = show.requestList[show.request.id]; + const requestType = show.request.type; + switch (requestType) { + case 'ASSET_DETAILS': + return createCanonicalLink({ asset: show.assetList[requestId.key] }); + case 'CHANNEL': + return createCanonicalLink({ channel: show.channelList[requestId.key] }); + default: + return null; + } +}; + const returnSagaWithParams = (saga, params) => { return function * () { yield call(saga, params); }; }; - module.exports = (req, res) => { let context = {}; @@ -90,7 +104,15 @@ module.exports = (req, res) => { sagaMiddleware .run(boundSaga) .done - .then(() => renderPage(store) ); + .then(() => { + // redirect if request does not use canonical url + const canonicalUrl = getCanonicalUrlFromShow(store.getState().show); + if (canonicalUrl && canonicalUrl !== req.originalUrl) { + console.log(`redirecting ${req.originalUrl} to ${canonicalUrl}`); + res.redirect(canonicalUrl); + } + return renderPage(store) + }); } else { const store = createStore(Reducers); renderPage(store);