Files
FlockRadar/sw.js
T

63 lines
1.7 KiB
JavaScript

// Service Worker for FlockRadar Offline Driving & Live Updates
const CACHE_NAME = 'flockradar-v22';
const ASSETS_TO_CACHE = [
'./',
'./index.html',
'./css/styles.css?v=22',
'./js/icons.js?v=22',
'./js/app.js?v=22',
'./js/auth.js?v=22',
'./js/radar.js?v=22',
'./data/portals.js?v=22',
'./favicon.svg?v=22',
'./favicon.ico?v=22',
'./apple-touch-icon.png?v=22',
'./icon-192.png?v=22',
'./icon-512.png?v=22',
'./og-image.png?v=22',
'./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);
})
);
});