From 7810140f15fcd9bed0eee10570047bec59431f1b Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Thu, 7 Feb 2019 22:08:36 +0100 Subject: [PATCH] Avoided site.changed event if nothing has changed refs #10461 --- core/server/api/v2/posts.js | 2 +- core/server/api/v2/tags.js | 10 +++++++--- core/server/api/v2/users.js | 10 +++++++--- core/test/regression/api/v2/admin/posts_spec.js | 9 ++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/core/server/api/v2/posts.js b/core/server/api/v2/posts.js index 1ebc19cfab..0237571aa4 100644 --- a/core/server/api/v2/posts.js +++ b/core/server/api/v2/posts.js @@ -131,7 +131,7 @@ module.exports = { query(frame) { return models.Post.edit(frame.data.posts[0], frame.options) .then((model) => { - if (model.get('status') === 'published' || + if (model.get('status') === 'published' && model.wasChanged() || model.get('status') === 'draft' && model.previous('status') === 'published') { this.headers.cacheInvalidate = true; } else if (model.get('status') === 'draft' && model.previous('status') !== 'published') { diff --git a/core/server/api/v2/tags.js b/core/server/api/v2/tags.js index 1317cc6884..f189d6abf3 100644 --- a/core/server/api/v2/tags.js +++ b/core/server/api/v2/tags.js @@ -91,9 +91,7 @@ module.exports = { }, edit: { - headers: { - cacheInvalidate: true - }, + headers: {}, options: [ 'id', 'include' @@ -118,6 +116,12 @@ module.exports = { })); } + if (model.wasChanged()) { + this.headers.cacheInvalidate = true; + } else { + this.headers.cacheInvalidate = false; + } + return model; }); } diff --git a/core/server/api/v2/users.js b/core/server/api/v2/users.js index e45551adc0..a28a1dccb9 100644 --- a/core/server/api/v2/users.js +++ b/core/server/api/v2/users.js @@ -69,9 +69,7 @@ module.exports = { }, edit: { - headers: { - cacheInvalidate: true - }, + headers: {}, options: [ 'id', 'include' @@ -98,6 +96,12 @@ module.exports = { })); } + if (model.wasChanged()) { + this.headers.cacheInvalidate = true; + } else { + this.headers.cacheInvalidate = false; + } + return model; }); } diff --git a/core/test/regression/api/v2/admin/posts_spec.js b/core/test/regression/api/v2/admin/posts_spec.js index 6c1d9d097e..4dfb518a13 100644 --- a/core/test/regression/api/v2/admin/posts_spec.js +++ b/core/test/regression/api/v2/admin/posts_spec.js @@ -125,14 +125,15 @@ describe('Posts API', function () { .expect('Cache-Control', testUtils.cacheRules.private) .expect(200) .then((res) => { - res.headers['x-cache-invalidate'].should.eql('/*'); + // @NOTE: you cannot modify published_at manually, that's why the resource won't change. + should.not.exist(res.headers['x-cache-invalidate']); should.exist(res.body.posts); should.exist(res.body.posts[0].published_at); localUtils.API.checkResponse(res.body.posts[0], 'post'); }); }); - it('update dates', function () { + it('update dates & x_by', function () { const post = { created_by: ObjectId.generate(), updated_by: ObjectId.generate(), @@ -148,7 +149,9 @@ describe('Posts API', function () { .expect('Cache-Control', testUtils.cacheRules.private) .expect(200) .then((res) => { - res.headers['x-cache-invalidate'].should.eql('/*'); + // @NOTE: you cannot modify these fields above manually, that's why the resource won't change. + should.not.exist(res.headers['x-cache-invalidate']); + localUtils.API.checkResponse(res.body.posts[0], 'post'); return models.Post.findOne({