43 lines
1022 B
JavaScript
43 lines
1022 B
JavaScript
const CACHE_NAME = 'faultcodes-v1';
|
|
const ASSETS = [
|
|
'/',
|
|
'/static/styles.css',
|
|
'/static/app.js',
|
|
'/static/manifest.webmanifest'
|
|
];
|
|
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil((async () => {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
await cache.addAll(ASSETS);
|
|
})());
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (e) => {
|
|
e.waitUntil((async () => {
|
|
const keys = await caches.keys();
|
|
await Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)));
|
|
})());
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (e) => {
|
|
const req = e.request;
|
|
if (req.method !== 'GET') return;
|
|
e.respondWith((async () => {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
const cached = await cache.match(req);
|
|
if (cached) return cached;
|
|
try {
|
|
const fresh = await fetch(req);
|
|
if (fresh && fresh.ok) cache.put(req, fresh.clone());
|
|
return fresh;
|
|
} catch {
|
|
return cached || Response.error();
|
|
}
|
|
})());
|
|
});
|
|
|
|
|