From 8a2862590bb76d90597fe02432ac2e01c70d26a2 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Fri, 7 Aug 2026 12:32:48 +0200 Subject: Erste Version --- app/service-worker.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 app/service-worker.js (limited to 'app/service-worker.js') diff --git a/app/service-worker.js b/app/service-worker.js new file mode 100644 index 0000000..18c0348 --- /dev/null +++ b/app/service-worker.js @@ -0,0 +1,57 @@ +/* Altersvorsorge-Vergleich — Service Worker + * + * Strategie: stale-while-revalidate für alles im Scope. + * Die App ist eine einzige statische Datei ohne Backend; Korrektheit hängt + * also nicht an Frische, wohl aber Verfügbarkeit. Deshalb wird sofort aus + * dem Cache geliefert und im Hintergrund aktualisiert. Beim Aktivieren + * werden alte Cache-Versionen gelöscht. + * + * Beim Ändern der App die Version hochzählen — sonst sieht ein bereits + * installierter Client die Änderung erst nach zwei Aufrufen. + */ +const VERSION = 'av-v3'; +const ASSETS = [ + './', + './index.html', + './manifest.json', + './icon-192.png', + './icon-512.png', + './icon-maskable-512.png' +]; + +self.addEventListener('install', (e) => { + e.waitUntil( + caches.open(VERSION) + .then((c) => c.addAll(ASSETS).catch(() => c.add('./index.html'))) + .then(() => self.skipWaiting()) + ); +}); + +self.addEventListener('activate', (e) => { + e.waitUntil( + caches.keys() + .then((ks) => Promise.all(ks.filter((k) => k !== VERSION).map((k) => caches.delete(k)))) + .then(() => self.clients.claim()) + ); +}); + +self.addEventListener('fetch', (e) => { + const req = e.request; + if (req.method !== 'GET') return; + const url = new URL(req.url); + if (url.origin !== location.origin) return; + + e.respondWith( + caches.open(VERSION).then((cache) => + cache.match(req, { ignoreSearch: true }).then((hit) => { + const net = fetch(req) + .then((res) => { + if (res && res.status === 200 && res.type === 'basic') cache.put(req, res.clone()); + return res; + }) + .catch(() => hit || cache.match('./index.html')); + return hit || net; + }) + ) + ); +}); -- cgit v1.2.1