diff --git a/CHANGELOG.md b/CHANGELOG.md index 26025a4e..828dccc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,14 @@ 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.24] - 2023-10-05 ### Added - Allow disabling memoization for all requests of a client + ## [6.45.23] - 2023-10-04 ### Fixed 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,