Ghost/core/frontend/apps/amp/index.js
Hannah Wolfe ce563179b8 🐛 Fixed infinite redirect for amp when disabled
fixes 10883

- fixed an issue where /amp/ pages would cause an infinite redirect loop
- this only occurred when amp was disabled, and query params were passed to the /amp/ url
- this fix resolves the issue by not assuming /amp/ is the end of the URL
- it also checks for `/amp/` (both slashes) and replaces one
2019-07-08 17:32:12 +01:00

29 lines
775 B
JavaScript

const router = require('./lib/router');
const registerHelpers = require('./lib/helpers');
const urlUtils = require('../../../server/lib/url-utils');
// Dirty requires
const settingsCache = require('../../../server/services/settings/cache');
function ampRouter(req, res) {
if (settingsCache.get('amp') === true) {
return router.apply(this, arguments);
} else {
// routeKeywords.amp: 'amp'
let redirectUrl = req.originalUrl.replace(/\/amp\//, '/');
urlUtils.redirect301(res, redirectUrl);
}
}
module.exports = {
activate: function activate(ghost) {
// routeKeywords.amp: 'amp'
let ampRoute = '*/amp/';
ghost.routeService.registerRouter(ampRoute, ampRouter);
registerHelpers(ghost);
}
};