Files
breccia-mapper/breccia_mapper/static/js/serviceworker.js
Matthew Grove 555aae5d1c [FEAT] Isolate settings from image build and allow PWA customisation
Settings can now be changed without rebuilding the image
PWA settings can now be changed
2023-02-10 13:32:13 +00:00

47 lines
1.4 KiB
JavaScript

var staticCacheName = "django-pwa-v" + new Date().getTime();
var filesToCache = [
"/offline",
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/fontawesome.min.css",
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/solid.min.css",
"/static/css/global.css",
"/static/hijack/hijack.min.css",
"/media/icon-192x192.png",
];
// Cache on install
self.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
)
});
// Clear cache on activate
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => (cacheName.startsWith("django-pwa-")))
.filter(cacheName => (cacheName !== staticCacheName))
.map(cacheName => caches.delete(cacheName))
);
})
);
});
// Serve from Cache
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request)
.then(response => {
return response || fetch(event.request);
})
.catch(() => {
return caches.match('offline');
})
)
});