🐛 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
This commit is contained in:
Hannah Wolfe 2019-07-08 17:29:41 +01:00
parent bc8f8979c1
commit ce563179b8
2 changed files with 21 additions and 6 deletions

View File

@ -1,16 +1,17 @@
const router = require('./lib/router'),
registerHelpers = require('./lib/helpers'),
urlUtils = require('../../../server/lib/url-utils'),
const router = require('./lib/router');
const registerHelpers = require('./lib/helpers');
const urlUtils = require('../../../server/lib/url-utils');
// Dirty requires
settingsCache = require('../../../server/services/settings/cache');
// 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\/$/, '');
let redirectUrl = req.originalUrl.replace(/\/amp\//, '/');
urlUtils.redirect301(res, redirectUrl);
}
}

View File

@ -262,6 +262,20 @@ describe('Frontend Routing', function () {
.expect(301)
.end(doEnd(done));
});
it('should redirect to regular post with query params when AMP is disabled', function (done) {
sinon.stub(settingsCache, 'get').callsFake(function (key, options) {
if (key === 'amp' && !options) {
return false;
}
return origCache.get(key, options);
});
request.get('/welcome/amp/?q=a')
.expect('Location', '/welcome/?q=a')
.expect(301)
.end(doEnd(done));
});
});
describe('Static assets', function () {