Version-stamped static URLs cached forever, thumbnail cache 25000 with throttled trimming

This commit is contained in:
Claude
2026-10-02 02:11:38 +00:00
parent c69cc3a4bc
commit 1b10a994dd
2 changed files with 18 additions and 6 deletions

View File

@@ -46,7 +46,7 @@ const THUMB_HOSTS = new Set([
]);
// Thumbnails accumulate with every card ever scrolled past — keep it bounded.
const THUMB_CACHE_MAX = 2500;
const THUMB_CACHE_MAX = 25000;
function isVersionedShellCache(key) {
return key.startsWith(SHELL_CACHE_PREFIX) && !UTILITY_CACHES.has(key);
}
@@ -222,7 +222,10 @@ self.addEventListener('message', (e) => {
// Cache-first: serve from cache; if missing, fetch, cache, return.
async function cacheFirst(request) {
const cache = await caches.open(CACHE);
const cached = await cache.match(request);
// Shell files are precached under their plain URL; index.html asks for them as
// `file?v=<build>` (so the HTTP cache can keep them forever) — match ignoring it.
const versioned = /[?&]v=/.test(request.url);
const cached = await cache.match(request, versioned ? { ignoreSearch: true } : undefined);
if (cached) return cached;
try {
const response = await fetch(request);
@@ -262,7 +265,7 @@ async function thumbnail(request) {
const cors = await fetch(request.url, { mode: 'cors', credentials: 'omit' });
if (cors && cors.ok) {
await cache.put(request, cors.clone()).catch(() => {});
trimThumbCache();
if (++_thumbPuts % 100 === 0) trimThumbCache();
return cors;
}
} catch { /* CORS refused or offline — fall through to the raw request */ }
@@ -271,7 +274,8 @@ async function thumbnail(request) {
const raw = await fetch(request);
if (raw && (raw.status === 200 || raw.status === 0)) {
await cache.put(request, raw.clone()).catch(() => {});
trimThumbCache();
// keys() on a 25k-entry cache is not free — trim every 100th put, not each one.
if (++_thumbPuts % 100 === 0) trimThumbCache();
}
return raw;
} catch {
@@ -282,6 +286,7 @@ async function thumbnail(request) {
// Cache.keys() is insertion-ordered, so the front of the list is the oldest.
let _trimmingThumbs = false;
let _thumbPuts = 0;
async function trimThumbCache() {
if (_trimmingThumbs) return;
_trimmingThumbs = true;