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 }; } 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) { var returnSagaWithParams = function returnSagaWithParams(saga, params) {
return ( return (
/*#__PURE__*/ /*#__PURE__*/
@ -115,6 +137,14 @@ module.exports = function (req, res) {
var boundSaga = returnSagaWithParams(saga, boundAction); // run the saga middleware with the saga call var boundSaga = returnSagaWithParams(saga, boundAction); // run the saga middleware with the saga call
sagaMiddleware.run(boundSaga).done.then(function () { 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); return renderPage(store);
}); });
} else { } else {

View file

@ -15,12 +15,26 @@ import App from '@app';
import Sagas from '@sagas'; import Sagas from '@sagas';
import Actions from '@actions'; 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) => { const returnSagaWithParams = (saga, params) => {
return function * () { return function * () {
yield call(saga, params); yield call(saga, params);
}; };
}; };
module.exports = (req, res) => { module.exports = (req, res) => {
let context = {}; let context = {};
@ -90,7 +104,15 @@ module.exports = (req, res) => {
sagaMiddleware sagaMiddleware
.run(boundSaga) .run(boundSaga)
.done .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 { } else {
const store = createStore(Reducers); const store = createStore(Reducers);
renderPage(store); renderPage(store);