57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
|
|
/**
|
||
|
|
* OpusBike Service Worker — Cache static assets for PWA
|
||
|
|
*/
|
||
|
|
|
||
|
|
const CACHE_NAME = 'opusbike-v3';
|
||
|
|
const STATIC_ASSETS = [
|
||
|
|
'./',
|
||
|
|
'./index.html',
|
||
|
|
'./css/style.css',
|
||
|
|
'./js/boschProtocol.js',
|
||
|
|
'./js/bleService.js',
|
||
|
|
'./js/wsRelay.js',
|
||
|
|
'./js/app.js',
|
||
|
|
'./manifest.json',
|
||
|
|
'./icons/icon-192.png',
|
||
|
|
'./icons/icon-512.png',
|
||
|
|
'./icons/apple-touch-icon.png',
|
||
|
|
'./icons/favicon.svg',
|
||
|
|
'./icons/proactys-logo.png',
|
||
|
|
'./icons/qr-code.png'
|
||
|
|
];
|
||
|
|
|
||
|
|
// Install — cache static assets
|
||
|
|
self.addEventListener('install', (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.open(CACHE_NAME)
|
||
|
|
.then(cache => cache.addAll(STATIC_ASSETS))
|
||
|
|
.then(() => self.skipWaiting())
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Activate — clean old caches
|
||
|
|
self.addEventListener('activate', (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.keys().then(keys =>
|
||
|
|
Promise.all(
|
||
|
|
keys.filter(key => key !== CACHE_NAME)
|
||
|
|
.map(key => caches.delete(key))
|
||
|
|
)
|
||
|
|
).then(() => self.clients.claim())
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Fetch — cache-first for static assets, network-first for everything else
|
||
|
|
self.addEventListener('fetch', (event) => {
|
||
|
|
const url = new URL(event.request.url);
|
||
|
|
|
||
|
|
// Skip WebSocket requests
|
||
|
|
if (url.pathname.endsWith('/ws')) return;
|
||
|
|
|
||
|
|
// Cache-first for known assets
|
||
|
|
event.respondWith(
|
||
|
|
caches.match(event.request)
|
||
|
|
.then(cached => cached || fetch(event.request))
|
||
|
|
);
|
||
|
|
});
|