From 001f2b0b9112056913b408896881606068b70584 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 28 Feb 2024 12:12:01 -0500 Subject: [PATCH] Invalidated post comments cache on like&unlike refs https://linear.app/tryghost/issue/ENG-676/ We want to make sure that we're not serving stale liked counts for comments, which means we need to cache bust when they're liked/unliked Unfortuantely this means we need to fetch the comment from the db so that we have access to the post id. --- .../services/comments/CommentsController.js | 21 +++++++++++++++++-- .../__snapshots__/comments.test.js.snap | 3 +++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ghost/core/core/server/services/comments/CommentsController.js b/ghost/core/core/server/services/comments/CommentsController.js index 8184219e27..df2497bfe5 100644 --- a/ghost/core/core/server/services/comments/CommentsController.js +++ b/ghost/core/core/server/services/comments/CommentsController.js @@ -152,11 +152,20 @@ module.exports = class CommentsController { async like(frame) { this.#checkMember(frame); - return await this.service.likeComment( + const result = await this.service.likeComment( frame.options.id, frame.options?.context?.member, frame.options ); + + const comment = await this.service.getCommentByID(frame.options.id); + + const postId = comment?.get('post_id'); + if (postId) { + frame.setHeader('X-Cache-Invalidate', `/api/members/comments/post/${postId}/`); + } + + return result; } /** @@ -165,11 +174,19 @@ module.exports = class CommentsController { async unlike(frame) { this.#checkMember(frame); - return await this.service.unlikeComment( + const result = await this.service.unlikeComment( frame.options.id, frame.options?.context?.member, frame.options ); + + const comment = await this.service.getCommentByID(frame.options.id); + + const postId = comment?.get('post_id'); + if (postId) { + frame.setHeader('X-Cache-Invalidate', `/api/members/comments/post/${postId}/`); + } + return result; } /** diff --git a/ghost/core/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap b/ghost/core/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap index 95a3eb91ed..be79c1f039 100644 --- a/ghost/core/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap +++ b/ghost/core/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap @@ -2027,6 +2027,7 @@ Object { "access-control-allow-origin": "*", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "x-cache-invalidate": "/api/members/comments/post/618ba1ffbe2896088840a6df/", "x-powered-by": "Express", } `; @@ -2094,6 +2095,7 @@ Object { "access-control-allow-origin": "*", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "x-cache-invalidate": "/api/members/comments/post/618ba1ffbe2896088840a6df/", "x-powered-by": "Express", } `; @@ -2260,6 +2262,7 @@ Object { "access-control-allow-origin": "*", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "x-cache-invalidate": "/api/members/comments/post/618ba1ffbe2896088840a6df/", "x-powered-by": "Express", } `;