From 44a16aa1aa51891418d1ff367f0e6ecfd7c37a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Thu, 5 Oct 2023 11:14:45 -0300 Subject: [PATCH 1/2] Stop updating client cache when revalidated and still expired --- src/HttpClient/middlewares/cache.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index 2d36e40c..d44d3dd9 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -198,7 +198,16 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => { ? (data as Buffer).toString(responseEncoding) : data - const expiration = Date.now() + (maxAge - currentAge) * 1000 + const now = Date.now() + const expiration = now + (maxAge - currentAge) * 1000 + + const alreadyExpired = expiration <= now + const reusingRevalidatedCache = cached && (ctx.response === cached.response) + const shouldSkipCacheUpdate = alreadyExpired && reusingRevalidatedCache + if (shouldSkipCacheUpdate) { + return + } + await storage.set(setKey, { etag, expiration, From 073077627037bf3741b0f214851d5fa8e3c6f926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Wed, 25 Oct 2023 11:06:02 -0300 Subject: [PATCH 2/2] Add entry to CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f11fdf2a..83258167 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Changed +- Stop updating client cache when revalidated and still expired + ## [6.45.23] - 2023-10-04 ### Fixed