Skip to content

Commit be173eb

Browse files
committed
fix: respect expired server TTL and use current token for cache storage
- Fix fallback TTL overriding explicitly expired server cache headers. Changed isEntryFresh() to use serverTtl !== 0 (catches both positive and negative values) instead of serverTtl > 0 (missed negative/expired). Same fix in collectEntryMetadata() with explicit < 0 branch. - Fix cache storing stale token after 401 refresh. Use getAuthToken() (reads current DB value) instead of the captured token variable when building cache request headers, so post-refresh tokens are stored.
1 parent 414d870 commit be173eb

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/lib/response-cache.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,16 @@ function isEntryFresh(
232232
return true;
233233
}
234234

235-
// CachePolicy says stale — check if we should override with fallback TTL
235+
// CachePolicy says stale — check if we should override with fallback TTL.
236+
// timeToLive() returns 0 when the server sent no cache headers, or a
237+
// positive/negative value when it did (negative = already expired).
236238
const serverTtl = policy.timeToLive();
237-
if (serverTtl > 0) {
238-
// Server provided a TTL and it expired — respect the server
239+
if (serverTtl !== 0) {
240+
// Server provided an explicit TTL — respect it (even if expired)
239241
return false;
240242
}
241243

242-
// No server TTL — use our fallback tier
244+
// No server TTL (0) — use our URL-based fallback tier
243245
const tier = classifyUrl(url);
244246
const fallbackTtl = FALLBACK_TTL_MS[tier];
245247
const age = Date.now() - entry.createdAt;
@@ -516,12 +518,17 @@ async function collectEntryMetadata(
516518
const entry = JSON.parse(raw) as CacheEntry;
517519
const policy = CachePolicy.fromObject(entry.policy);
518520

521+
// timeToLive() returns 0 when the server sent no cache headers,
522+
// positive when still fresh, negative when explicitly expired.
519523
const serverTtl = policy.timeToLive();
520524
let expired: boolean;
521525
if (serverTtl > 0) {
522526
expired = false;
527+
} else if (serverTtl < 0) {
528+
// Server-provided TTL has expired — don't override with fallback
529+
expired = true;
523530
} else {
524-
// Use the entry's stored URL for accurate tier classification
531+
// No server TTL (0) — use URL-based fallback tier
525532
const tier = classifyUrl(entry.url ?? "");
526533
expired = now - entry.createdAt > FALLBACK_TTL_MS[tier];
527534
}

src/lib/sentry-client.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,14 @@ async function fetchWithRetry(
280280
const result = await executeAttempt(input, init, headers, isLastAttempt);
281281

282282
if (result.action === "done") {
283-
cacheResponse(method, fullUrl, authHeaders(token), result.response);
283+
// Use getAuthToken() instead of captured `token` — after a 401 refresh,
284+
// handleUnauthorized stores a new token in the DB
285+
cacheResponse(
286+
method,
287+
fullUrl,
288+
authHeaders(getAuthToken()),
289+
result.response
290+
);
284291
return result.response;
285292
}
286293
if (result.action === "throw") {

0 commit comments

Comments
 (0)