63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
// Service Worker for FlockRadar Offline Driving & Live Updates
|
|
const CACHE_NAME = 'flockradar-v20';
|
|
const ASSETS_TO_CACHE = [
|
|
'./',
|
|
'./index.html',
|
|
'./css/styles.css?v=20',
|
|
'./js/icons.js?v=20',
|
|
'./js/app.js?v=20',
|
|
'./js/auth.js?v=20',
|
|
'./js/radar.js?v=20',
|
|
'./data/portals.js?v=20',
|
|
'./favicon.svg?v=20',
|
|
'./favicon.ico?v=20',
|
|
'./apple-touch-icon.png?v=20',
|
|
'./icon-192.png?v=20',
|
|
'./icon-512.png?v=20',
|
|
'./og-image.png?v=20',
|
|
'./assets/img/logo.svg',
|
|
'./assets/img/flock_pole_cam_palmdale.jpg',
|
|
'./manifest.json'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
self.skipWaiting();
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then(cache => cache.addAll(ASSETS_TO_CACHE))
|
|
);
|
|
});
|
|
|
|
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())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
// Always network-first for HTML, JS and CSS to ensure instant updates
|
|
const url = new URL(event.request.url);
|
|
if (url.origin === self.location.origin) {
|
|
event.respondWith(
|
|
fetch(event.request, { cache: 'no-cache' })
|
|
.then(response => {
|
|
if (response && response.status === 200) {
|
|
const clone = response.clone();
|
|
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
|
|
}
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(event.request))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Fallback for CDN resources (Leaflet tiles etc)
|
|
event.respondWith(
|
|
caches.match(event.request).then(cached => {
|
|
return cached || fetch(event.request);
|
|
})
|
|
);
|
|
});
|