Skip to content

Commit

Permalink
Stop updating client cache when revalidated and still expired
Browse files Browse the repository at this point in the history
  • Loading branch information
mairatma committed Oct 5, 2023
1 parent dcda99c commit 44a16aa
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/HttpClient/middlewares/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 44a16aa

Please sign in to comment.