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

🐛 Fixed post redirect with query params

no issue

- reported here: https://forum.ghost.org/t/best-practice-for-appending-a-query-string-to-a-post/1535
- there was a bug that query params were not respected and this ended in a 301 redirect losing them
This commit is contained in:
kirrg001 2018-06-07 09:52:51 +02:00
parent 3155ea2aa7
commit 839d82b398
2 changed files with 39 additions and 2 deletions

View file

@ -1,4 +1,5 @@
const debug = require('ghost-ignition').debug('services:routing:controllers:entry'),
url = require('url'),
urlService = require('../../url'),
filters = require('../../../filters'),
helpers = require('../helpers');
@ -63,10 +64,18 @@ module.exports = function entryController(req, res, next) {
*
* The resource url always contains the subdirectory. This was different before dynamic routing.
* That's why we have to use the original url, which contains the sub-directory.
*
* @NOTE
*
* post.url contains the subdirectory if configured.
*/
if (post.url !== req.originalUrl) {
if (post.url !== url.parse(req.originalUrl).pathname) {
debug('redirect');
return urlService.utils.redirect301(res, post.url);
return urlService.utils.redirect301(res, url.format({
pathname: post.url,
search: url.parse(req.originalUrl).search
}));
}
helpers.secure(req, post);

View file

@ -177,5 +177,33 @@ describe('Unit - services/routing/controllers/entry', function () {
controllers.entry(req, res);
});
it('requested url !== resource url: with query params', function (done) {
post.url = '/2017/08/' + post.url;
req.path = '/2017/07/' + post.url;
req.originalUrl = req.path + '?query=true';
res.locals.routerOptions.type = 'posts';
urlService.getResource.withArgs(post.url).returns({
config: {
type: 'posts'
}
});
postLookUpStub.withArgs(req.path, res.locals.routerOptions)
.resolves({
post: post
});
urlService.utils.redirect301.callsFake(function (res, postUrl) {
postUrl.should.eql(post.url + '?query=true');
done();
});
controllers.entry(req, res, function (err) {
done(err);
});
});
});
});