0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Redirected /amp links to original post when AMP is disabled (#9496)

closes #9495

- Added a clause for amp being disabled
- In this clause, we strip the final 'amp/' part of the url, and redirect
- Changed corresponding test in frontend_spec.js
- Used `urlService.utils.redirect301()` instead of `res.redirect()`
This commit is contained in:
Rosco Kalis 2018-03-19 10:11:48 +01:00 committed by Aileen Nowak
parent 7ed822cc0c
commit 3d8bf02a8d
2 changed files with 14 additions and 10 deletions

View file

@ -1,21 +1,25 @@
var router = require('./lib/router'),
registerHelpers = require('./lib/helpers'),
urlService = require('../../services/url'),
// Dirty requires
config = require('../../config'),
settingsCache = require('../../services/settings/cache');
function ampRouter(req, res) {
if (settingsCache.get('amp') === true) {
return router.apply(this, arguments);
} else {
var redirectUrl = req.originalUrl.replace(/amp\/$/, '');
urlService.utils.redirect301(res, redirectUrl);
}
}
module.exports = {
activate: function activate(ghost) {
var ampRoute = '*/' + config.get('routeKeywords').amp + '/';
ghost.routeService.registerRouter(ampRoute, function settingsEnabledRouter(req, res, next) {
if (settingsCache.get('amp') === true) {
return router.apply(this, arguments);
}
next();
});
ghost.routeService.registerRouter(ampRoute, ampRouter);
registerHelpers(ghost);
}

View file

@ -321,7 +321,7 @@ describe('Frontend Routing', function () {
.end(doEnd(done));
});
it('should not render AMP, when AMP is disabled', function (done) {
it('should redirect to regular post when AMP is disabled', function (done) {
sandbox.stub(settingsCache, 'get').callsFake(function (key, options) {
if (key === 'amp' && !options) {
return false;
@ -330,8 +330,8 @@ describe('Frontend Routing', function () {
});
request.get('/welcome/amp/')
.expect(404)
.expect(/Page not found/)
.expect('Location', '/welcome/')
.expect(301)
.end(doEnd(done));
});
});