2020-11-26 08:20:34 +00:00
|
|
|
const staticCacheName = 'site-static-v2';
|
|
|
|
const dynamicCacheName = 'site-dynamic-v1';
|
|
|
|
const assets = [
|
2020-11-27 10:27:21 +00:00
|
|
|
'/offline.html',
|
2020-11-26 08:20:34 +00:00
|
|
|
'/css/app.css',
|
|
|
|
'/css/normalize.css',
|
|
|
|
'/css/webapp.css',
|
|
|
|
'/css/webappdark.css',
|
2020-11-27 10:27:21 +00:00
|
|
|
'/images/icons/Vagttelefon-normal.svg',
|
|
|
|
'/images/icons/Vagttelefon-hvid.svg',
|
|
|
|
'/images/icons/about.svg',
|
|
|
|
'/images/icons/Aktiviteter.svg',
|
|
|
|
'/images/icons/Home.svg',
|
|
|
|
'/images/icons/Kontoret.svg',
|
|
|
|
'/images/icons/location.svg',
|
|
|
|
'/images/icons/Menuplan.svg',
|
|
|
|
'/images/icons/news.svg',
|
|
|
|
'/images/icons/phone.svg',
|
|
|
|
'/images/icons/questionmark.svg',
|
|
|
|
'/images/icons/settings-hvid.svg',
|
|
|
|
'/images/icons/user.svg',
|
|
|
|
'/images/icons/user-hvid.svg',
|
|
|
|
'/images/icons/Vejledninger.svg',
|
|
|
|
'/images/icons/watermelon.svg',
|
|
|
|
'/images/logos/Logo-hvid.svg',
|
|
|
|
'/images/logos/Logo-normal.svg',
|
|
|
|
'/images/icons/Logout.svg'
|
2020-11-26 08:20:34 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// install event
|
|
|
|
self.addEventListener('install', evt => {
|
2020-11-30 14:14:11 +00:00
|
|
|
console.log('service worker installed');
|
2020-11-26 08:20:34 +00:00
|
|
|
evt.waitUntil(
|
|
|
|
caches.open(staticCacheName).then((cache) => {
|
2020-11-26 11:44:53 +00:00
|
|
|
//console.log('caching shell assets');
|
2020-11-26 08:20:34 +00:00
|
|
|
return cache.addAll(assets);
|
2020-11-27 10:27:21 +00:00
|
|
|
}).catch((r) => {
|
2020-11-30 14:14:11 +00:00
|
|
|
console.log("Install Fail");
|
2020-11-26 08:20:34 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// activate event
|
|
|
|
self.addEventListener('activate', evt => {
|
2020-11-30 14:14:11 +00:00
|
|
|
console.log('service worker activated');
|
2020-11-26 08:20:34 +00:00
|
|
|
evt.waitUntil(
|
|
|
|
caches.keys().then(keys => {
|
|
|
|
//console.log(keys);
|
|
|
|
return Promise.all(keys
|
|
|
|
.filter(key => key !== staticCacheName && key !== dynamicCacheName)
|
|
|
|
.map(key => caches.delete(key))
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2020-11-30 14:14:11 +00:00
|
|
|
|
2020-11-26 08:20:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('fetch', evt => {
|
2020-11-26 11:44:53 +00:00
|
|
|
console.log('fetch event');
|
2020-11-30 20:13:20 +00:00
|
|
|
|
|
|
|
if(!navigator.onLine)
|
|
|
|
evt.respondWith(
|
|
|
|
caches.match(evt.request).then(cacheRes => {
|
|
|
|
return cacheRes || caches.match('/offline.html');
|
|
|
|
})
|
|
|
|
);
|
|
|
|
else
|
|
|
|
evt.respondWith(
|
|
|
|
fetch(evt.request).then(fetchRes => {
|
2020-11-26 08:20:34 +00:00
|
|
|
return caches.open(dynamicCacheName).then(cache => {
|
2020-11-27 10:27:21 +00:00
|
|
|
cache.put(evt.request.url, fetchRes.clone());
|
|
|
|
return fetchRes;
|
2020-11-26 08:20:34 +00:00
|
|
|
})
|
2020-11-30 20:13:20 +00:00
|
|
|
})
|
|
|
|
);
|
2020-11-26 08:20:34 +00:00
|
|
|
});
|
|
|
|
|
2020-11-30 14:14:11 +00:00
|
|
|
self.addEventListener('push', function(event) {
|
|
|
|
if (!(self.Notification && self.Notification.permission === 'granted')) {
|
|
|
|
//notifications aren't supported or permission not granted!
|
|
|
|
return;
|
2020-11-27 10:27:21 +00:00
|
|
|
}
|
2020-11-26 08:20:34 +00:00
|
|
|
|
2020-11-30 14:14:11 +00:00
|
|
|
if (event.data) {
|
|
|
|
let msg = event.data.json();
|
|
|
|
console.log(msg)
|
|
|
|
event.waitUntil(self.registration.showNotification(msg.title, {
|
|
|
|
body: msg.body,
|
|
|
|
icon: msg.icon,
|
2020-12-01 07:35:53 +00:00
|
|
|
badge: msg.badge,
|
2020-11-30 14:14:11 +00:00
|
|
|
actions: msg.actions
|
|
|
|
}));
|
2020-11-26 08:20:34 +00:00
|
|
|
}
|
2020-11-30 14:14:11 +00:00
|
|
|
});
|
2020-12-01 07:35:53 +00:00
|
|
|
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
|
|
event.notification.close();
|
|
|
|
|
|
|
|
if (event.action === 'some_action') {
|
|
|
|
self.clients.openWindow('/home');
|
|
|
|
} else {
|
|
|
|
self.clients.openWindow('/')
|
|
|
|
}
|
|
|
|
});
|