Skip to content

Commit

Permalink
🐛 Fixed prev_next helper when using Content API v2 (#10397)
Browse files Browse the repository at this point in the history
closes #10389
  • Loading branch information
kirrg001 committed Jan 21, 2019
1 parent 6f87f2a commit a927aec
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 21 deletions.
11 changes: 8 additions & 3 deletions core/server/helpers/prev_next.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ fetch = function fetch(options, data) {
module.exports = function prevNext(options) {
options = options || {};

var data = createFrame(options.data);
const data = createFrame(options.data);
const context = options.data.root.context;

// Guard against incorrect usage of the helpers
if (!options.fn || !options.inverse) {
Expand All @@ -86,8 +87,12 @@ module.exports = function prevNext(options) {
return Promise.resolve();
}

// Guard against trying to execute prev/next on previews, pages, or other resources
if (!isPost(this) || this.status !== 'published' || this.page) {
if (context.includes('preview')) {
return Promise.resolve(options.inverse(this, {data: data}));
}

// Guard against trying to execute prev/next on pages, or other resources
if (!isPost(this) || this.page) {
return Promise.resolve(options.inverse(this, {data: data}));
}

Expand Down
3 changes: 2 additions & 1 deletion core/server/services/routing/PreviewRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class PreviewRouter extends ParentRouter {
_prepareContext(req, res, next) {
res.routerOptions = {
type: 'entry',
query: this.RESOURCE_CONFIG
query: this.RESOURCE_CONFIG,
context: ['preview']
};

next();
Expand Down
35 changes: 22 additions & 13 deletions core/server/services/routing/helpers/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const labs = require('../../labs'),
subscribePattern = new RegExp('^\\/subscribe\\/'),
// routeKeywords.amp: 'amp'
ampPattern = new RegExp('\\/amp\\/$'),
homePattern = new RegExp('^\\/$'),
previewPattern = new RegExp('^\\/p\\/');
homePattern = new RegExp('^\\/$');

function setResponseContext(req, res, data) {
var pageParam = req.params && req.params.page !== undefined ? parseInt(req.params.page, 10) : 1;
Expand Down Expand Up @@ -47,21 +46,31 @@ function setResponseContext(req, res, data) {
res.locals.context.push('amp');
}

if (previewPattern.test(res.locals.relativeUrl)) {
res.locals.context.push('preview');
}

// Each page can only have at most one of these
if (res.routerOptions && res.routerOptions.context) {
res.locals.context = res.locals.context.concat(res.routerOptions.context);
} else if (privatePattern.test(res.locals.relativeUrl)) {
res.locals.context.push('private');
} else if (subscribePattern.test(res.locals.relativeUrl) && labs.isSet('subscribers') === true) {
res.locals.context.push('subscribe');
} else if (data && data.post && data.post.page) {
res.locals.context.push('page');
}

if (privatePattern.test(res.locals.relativeUrl)) {
if (!res.locals.context.includes('private')) {
res.locals.context.push('private');
}
}

if (subscribePattern.test(res.locals.relativeUrl) && labs.isSet('subscribers') === true) {
if (!res.locals.context.includes('subscribe')) {
res.locals.context.push('subscribe');
}
}

if (data && data.post && data.post.page) {
if (!res.locals.context.includes('page')) {
res.locals.context.push('page');
}
} else if (data && data.post) {
res.locals.context.push('post');
if (!res.locals.context.includes('post')) {
res.locals.context.push('post');
}
}
}

Expand Down
29 changes: 27 additions & 2 deletions core/test/unit/helpers/next_post_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ describe('{{next_post}} helper', function () {
var browsePostStub;

beforeEach(function () {
locals = {root: {_locals: {apiVersion: 'v0.1'}}};
locals = {
root: {
_locals: {
apiVersion: 'v0.1'
},
context: ['post']
}
};
});

afterEach(function () {
Expand Down Expand Up @@ -130,6 +137,15 @@ describe('{{next_post}} helper', function () {

describe('for page', function () {
beforeEach(function () {
locals = {
root: {
_locals: {
apiVersion: 'v0.1'
},
context: ['page']
}
};

browsePostStub = sandbox.stub(api['v0.1'].posts, 'browse').callsFake(function (options) {
if (options.filter.indexOf('published_at:>') > -1) {
return Promise.resolve({posts: [{slug: '/previous/', title: 'post 1'}]});
Expand Down Expand Up @@ -165,6 +181,15 @@ describe('{{next_post}} helper', function () {

describe('for unpublished post', function () {
beforeEach(function () {
locals = {
root: {
_locals: {
apiVersion: 'v0.1'
},
context: ['preview', 'post']
}
};

browsePostStub = sandbox.stub(api['v0.1'].posts, 'browse').callsFake(function (options) {
if (options.filter.indexOf('published_at:>') > -1) {
return Promise.resolve({posts: [{slug: '/next/', title: 'post 3'}]});
Expand Down Expand Up @@ -406,7 +431,7 @@ describe('{{next_post}} helper', function () {
it('should show warning for call without any options', function (done) {
var fn = sinon.spy(),
inverse = sinon.spy(),
optionsData = {name: 'next_post'};
optionsData = {name: 'next_post', data: {root: {}}};

helpers.next_post
.call(
Expand Down
29 changes: 27 additions & 2 deletions core/test/unit/helpers/prev_post_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ describe('{{prev_post}} helper', function () {
let locals;

beforeEach(function () {
locals = {root: {_locals: {apiVersion: 'v0.1'}}};
locals = {
root: {
_locals: {
apiVersion: 'v0.1'
},
context: ['post']
}
};
});

afterEach(function () {
Expand Down Expand Up @@ -130,6 +137,15 @@ describe('{{prev_post}} helper', function () {

describe('for page', function () {
beforeEach(function () {
locals = {
root: {
_locals: {
apiVersion: 'v0.1'
},
context: ['page']
}
};

browsePostStub = sandbox.stub(api['v0.1'].posts, 'browse').callsFake(function (options) {
if (options.filter.indexOf('published_at:<=') > -1) {
return Promise.resolve({posts: [{slug: '/previous/', title: 'post 1'}]});
Expand Down Expand Up @@ -165,6 +181,15 @@ describe('{{prev_post}} helper', function () {

describe('for unpublished post', function () {
beforeEach(function () {
locals = {
root: {
_locals: {
apiVersion: 'v0.1'
},
context: ['preview', 'post']
}
};

browsePostStub = sandbox.stub(api['v0.1'].posts, 'browse').callsFake(function (options) {
if (options.filter.indexOf('published_at:<=') > -1) {
return Promise.resolve({posts: [{slug: '/previous/', title: 'post 1'}]});
Expand Down Expand Up @@ -406,7 +431,7 @@ describe('{{prev_post}} helper', function () {
it('should show warning for call without any options', function (done) {
var fn = sinon.spy(),
inverse = sinon.spy(),
optionsData = {name: 'prev_post'};
optionsData = {name: 'prev_post', data: {root: {}}};

helpers.prev_post
.call(
Expand Down

0 comments on commit a927aec

Please sign in to comment.