mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Fix context setup (remove dep on req.route.path)
refs #5344 - As a result of #5344, context are pretty broken. - This PR removes all dependence on req.route.path, and uses res.locals.relativeUrl - res.locals.relativeUrl is used for many things and is dependable
This commit is contained in:
parent
da9fcc7a19
commit
cc00c6c64a
3 changed files with 11 additions and 13 deletions
|
@ -68,25 +68,27 @@ function setResponseContext(req, res, data) {
|
||||||
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
||||||
tagPattern = new RegExp('^\\/' + config.routeKeywords.tag + '\\/'),
|
tagPattern = new RegExp('^\\/' + config.routeKeywords.tag + '\\/'),
|
||||||
authorPattern = new RegExp('^\\/' + config.routeKeywords.author + '\\/'),
|
authorPattern = new RegExp('^\\/' + config.routeKeywords.author + '\\/'),
|
||||||
privatePattern = new RegExp('^\\/' + config.routeKeywords.private + '\\/');
|
privatePattern = new RegExp('^\\/' + config.routeKeywords.private + '\\/'),
|
||||||
|
indexPattern = new RegExp('^\\/' + config.routeKeywords.page + '\\/'),
|
||||||
|
homePattern = new RegExp('^\\/$');
|
||||||
|
|
||||||
// paged context
|
// paged context
|
||||||
if (!isNaN(pageParam) && pageParam > 1) {
|
if (!isNaN(pageParam) && pageParam > 1) {
|
||||||
contexts.push('paged');
|
contexts.push('paged');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.route.path === '/' + config.routeKeywords.page + '/:page/') {
|
if (indexPattern.test(res.locals.relativeUrl)) {
|
||||||
contexts.push('index');
|
contexts.push('index');
|
||||||
} else if (req.route.path === '/') {
|
} else if (homePattern.test(res.locals.relativeUrl)) {
|
||||||
contexts.push('home');
|
contexts.push('home');
|
||||||
contexts.push('index');
|
contexts.push('index');
|
||||||
} else if (/\/rss\/(:page\/)?$/.test(req.route.path)) {
|
} else if (/^\/rss\//.test(res.locals.relativeUrl)) {
|
||||||
contexts.push('rss');
|
contexts.push('rss');
|
||||||
} else if (privatePattern.test(req.route.path)) {
|
} else if (privatePattern.test(res.locals.relativeUrl)) {
|
||||||
contexts.push('private');
|
contexts.push('private');
|
||||||
} else if (tagPattern.test(req.route.path)) {
|
} else if (tagPattern.test(res.locals.relativeUrl)) {
|
||||||
contexts.push('tag');
|
contexts.push('tag');
|
||||||
} else if (authorPattern.test(req.route.path)) {
|
} else if (authorPattern.test(res.locals.relativeUrl)) {
|
||||||
contexts.push('author');
|
contexts.push('author');
|
||||||
} else if (data && data.post && data.post.page) {
|
} else if (data && data.post && data.post.page) {
|
||||||
contexts.push('page');
|
contexts.push('page');
|
||||||
|
|
|
@ -14,10 +14,6 @@ var _ = require('lodash'),
|
||||||
getFeedXml,
|
getFeedXml,
|
||||||
feedCache = {};
|
feedCache = {};
|
||||||
|
|
||||||
function isPaginated(req) {
|
|
||||||
return req.route.path.indexOf(':page') !== -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isTag(req) {
|
function isTag(req) {
|
||||||
return req.originalUrl.indexOf('/' + config.routeKeywords.tag + '/') !== -1;
|
return req.originalUrl.indexOf('/' + config.routeKeywords.tag + '/') !== -1;
|
||||||
}
|
}
|
||||||
|
@ -209,7 +205,7 @@ generate = function (req, res, next) {
|
||||||
options = getOptions(req, pageParam, slugParam);
|
options = getOptions(req, pageParam, slugParam);
|
||||||
|
|
||||||
// No negative pages, or page 1
|
// No negative pages, or page 1
|
||||||
if (isNaN(pageParam) || pageParam < 1 || (pageParam === 1 && isPaginated(req))) {
|
if (isNaN(pageParam) || pageParam < 1 || (req.params.page !== undefined && pageParam === 1)) {
|
||||||
return res.redirect(baseUrl);
|
return res.redirect(baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1467,7 +1467,7 @@ describe('Frontend Controller', function () {
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
res = {
|
res = {
|
||||||
locals: {version: ''},
|
locals: {version: '', relativeUrl: '/private/'},
|
||||||
render: sandbox.spy()
|
render: sandbox.spy()
|
||||||
},
|
},
|
||||||
req = {
|
req = {
|
||||||
|
|
Loading…
Add table
Reference in a new issue