fix(sw): stop unbounded cache growth — delete stale caches on activate, cache only static assets

- activate: delete ALL caches except current STATIC/RUNTIME (was keeping every
  previous construction-delivery-* cache forever → 3GB storage, 30-40s caches.match)
- fetch: only cache same-origin static assets (js/css/img/fonts); API/data and
  cross-origin requests go straight to network, never cached
- bump to v115
This commit is contained in:
Hermes 2026-08-17 16:17:36 +02:00
parent 56cb4b460f
commit 981ca827a2
1 changed files with 11 additions and 3 deletions

View File

@ -1,10 +1,15 @@
const isLocalhost = self.location.hostname === "localhost" || self.location.hostname === "127.0.0.1";
if (!isLocalhost) {
const STATIC_CACHE = "construction-delivery-static-v114";
const RUNTIME_CACHE = "construction-delivery-runtime-v114";
const STATIC_CACHE = "construction-delivery-static-v115";
const RUNTIME_CACHE = "construction-delivery-runtime-v115";
const APP_SHELL_URLS = ["/", "/client", "/index.html", "/manifest.webmanifest", "/icons/icon-192.png", "/icons/icon-512.png"];
// Only these are treated as cacheable static assets. Everything else
// (API/data requests, cross-origin) goes straight to the network and is
// NEVER written to cache — prevents unbounded cache growth.
const ASSET_EXT = /\.(js|mjs|css|png|jpe?g|gif|svg|webp|ico|woff2?|ttf|eot|webmanifest)$/i;
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(STATIC_CACHE).then((cache) => cache.addAll(APP_SHELL_URLS)).then(() => self.skipWaiting()),
@ -38,6 +43,7 @@ if (!isLocalhost) {
const requestUrl = new URL(event.request.url);
const isSameOrigin = requestUrl.origin === self.location.origin;
// Navigation: network-first, fall back to cached index.html when offline.
if (event.request.mode === "navigate") {
event.respondWith(
fetch(event.request)
@ -54,7 +60,9 @@ if (!isLocalhost) {
return;
}
if (!isSameOrigin) {
// Only cache same-origin static assets (hashed build files, images, fonts).
// API/data requests and cross-origin calls are never cached.
if (!isSameOrigin || !ASSET_EXT.test(requestUrl.pathname)) {
return;
}