Files

32 lines
1.1 KiB
JavaScript

// sw.js — minimal offline cache so the PWA launches without a network.
const CACHE = 'babymobile-v9';
const ASSETS = [
'./', './index.html', './styles.css',
'./app.js', './ble.js', './wav.js',
'./manifest.webmanifest', './icon.svg',
];
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
.then(() => self.clients.claim())
);
});
// Cache-first for app shell; network passthrough otherwise.
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
if (e.request.method !== 'GET' || url.origin !== location.origin) return;
e.respondWith(
caches.match(e.request).then((hit) => hit || fetch(e.request).then((res) => {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(e.request, copy)).catch(() => {});
return res;
}).catch(() => hit))
);
});