From 680d6f401a66e1cb498a6d1bbd0ac8438702f862 Mon Sep 17 00:00:00 2001 From: Kresna Date: Sat, 1 Aug 2026 18:48:23 +0700 Subject: [PATCH] fix(pwa): cache ML model weights so they don't re-download on refresh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit transformers.js fetches Whisper weights from huggingface.co (cross-origin) and its own Cache Storage wasn't reliably persisting for users, so the model re-downloaded on every refresh. Add a service-worker CacheFirst runtimeCaching rule for the HF hosts (incl. cdn-lfs* / *.hf.co redirect CDNs) and cdn.jsdelivr (ORT wasm) — our SW now serves them from cache across refreshes. Co-Authored-By: Claude Opus 4.8 (1M context) --- astro.config.mjs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/astro.config.mjs b/astro.config.mjs index cf0d999..1250466 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -128,6 +128,23 @@ export default defineConfig({ statuses: [0, 200] } } + }, + { + // On-device ML model weights + runtime (Whisper from Hugging Face, ORT + // wasm from jsDelivr). CacheFirst so they load from our cache on refresh + // instead of re-downloading. Immutable, versioned by URL. + urlPattern: /^https:\/\/([^/]*\.)?(huggingface\.co|hf\.co)\/.*|^https:\/\/cdn\.jsdelivr\.net\/.*/i, + handler: 'CacheFirst', + options: { + cacheName: 'ml-models-cache', + expiration: { + maxEntries: 120, + maxAgeSeconds: 60 * 60 * 24 * 90 // 90 days + }, + cacheableResponse: { + statuses: [0, 200] + } + } } ] },