From ef38316976a9c460be0bf0b9da496fdfd939990a Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 23 Feb 2024 19:53:27 +0000 Subject: [PATCH] Added rounding to Comments-UI API fetch timestamp (#19750) no issue - allows for comments to be cached for 10s to greatly reduce the load hitting Ghost --- apps/comments-ui/src/utils/api.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/comments-ui/src/utils/api.ts b/apps/comments-ui/src/utils/api.ts index 87d6803354..339cb767e2 100644 --- a/apps/comments-ui/src/utils/api.ts +++ b/apps/comments-ui/src/utils/api.ts @@ -122,7 +122,11 @@ function setupGhostApi({siteUrl = window.location.origin, apiUrl, apiKey}: {site return json; }, browse({page, postId}: {page: number, postId: string}) { - firstCommentsLoadedAt = firstCommentsLoadedAt ?? new Date().toISOString(); + // use the most recent round 10sec timestamp to fetch comments so we can have some caching + const now = Date.now(); + const timestamp = (new Date(now - (now % 10000))).toISOString(); + + firstCommentsLoadedAt = firstCommentsLoadedAt ?? timestamp; const filter = encodeURIComponent(`post_id:'${postId}'+created_at:<=${firstCommentsLoadedAt}`); const order = encodeURIComponent('created_at DESC, id DESC');