Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop updating client cache when revalidated and still expired #549

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
arturpimentel marked this conversation as resolved.
Show resolved Hide resolved
const reusingRevalidatedCache = cached && (ctx.response === cached.response)
filafb marked this conversation as resolved.
Show resolved Hide resolved
const shouldSkipCacheUpdate = alreadyExpired && reusingRevalidatedCache
if (shouldSkipCacheUpdate) {
return
}

await storage.set(setKey, {
etag,
expiration,
Expand Down