redirect to canonical urls

This commit is contained in:
Travis Eden 2018-11-01 10:58:12 -04:00
parent 379835288c
commit 1833c326a4
2 changed files with 54 additions and 2 deletions

View file

@ -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 {

View file

@ -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);