Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Fix the service worker precache: drop missing files, cache per URL (MISC-01)

Omega (QA)d81b807783ec
diff --git a/frontend/sw.js b/frontend/sw.js
index 9889772..156863c 100644
--- a/frontend/sw.js
+++ b/frontend/sw.js
@@ -11,11 +11,9 @@ const urlsToCache = [
'./reset-password.html',
'./reset-password-request.html',
'./auth-redirect.html',
- './debug-export.html',
// Stylesheets (versioned)
'./style.css?v=20250119004',
- './styles.css?v=20250119001',
'./settings-styles.css?v=20250119001',
'./header-fix.css?v=20250119001',
'./mobile-header.css?v=20250119002',
@@ -57,7 +55,15 @@ self.addEventListener('install', event => {
caches.open(CACHE_NAME)
.then(cache => {
console.log('Opened cache');
- return cache.addAll(urlsToCache);
+ // Cache each URL on its own: cache.addAll is atomic, so one
+ // missing file used to roll back the whole batch and leave
+ // offline caching silently empty (MISC-01). A file that fails
+ // to cache now logs a warning instead of voiding the rest.
+ return Promise.all(urlsToCache.map(url =>
+ cache.add(url).catch(error => {
+ console.warn('Service worker: failed to precache ' + url, error);
+ })
+ ));
})
);
// Activate the new service worker immediately after installation
diff --git a/tests/test_service_worker.py b/tests/test_service_worker.py
new file mode 100644
index 0000000..7c302a7
--- /dev/null
+++ b/tests/test_service_worker.py
@@ -0,0 +1,39 @@
+"""MISC-01: every file the service worker precaches must exist.
+
+Two precache entries pointed at files that do not exist, and the
+atomic cache.addAll install rolled back the whole batch, leaving
+offline caching silently empty on plain static hosting. The install
+now caches per URL so one failure cannot void the rest, and this pin
+fails the suite if a precache entry ever goes stale again.
+"""
+import os
+import re
+
+REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+FRONTEND_DIR = os.path.join(REPO_ROOT, "frontend")
+
+
+def _precache_urls():
+ with open(os.path.join(FRONTEND_DIR, "sw.js"), encoding="utf-8") as fh:
+ src = fh.read()
+ block = src.split("urlsToCache")[1].split("];")[0]
+ return re.findall(r"'(\./[^']+)'", block), src
+
+
+def test_every_precache_entry_exists_on_disk():
+ urls, _ = _precache_urls()
+ assert urls, "no precache entries parsed from sw.js"
+ missing = []
+ for url in urls:
+ path = url[2:].split("?")[0]
+ if not path:
+ continue
+ if not os.path.isfile(os.path.join(FRONTEND_DIR, path)):
+ missing.append(url)
+ assert missing == []
+
+
+def test_install_caches_per_url_not_atomically():
+ _, src = _precache_urls()
+ assert "cache.addAll(urlsToCache)" not in src
+ assert "cache.add(url)" in src