105 lines
2.9 KiB
JavaScript
105 lines
2.9 KiB
JavaScript
const staticCacheName = 'site-static-v2';
|
|
const dynamicCacheName = 'site-dynamic-v1';
|
|
const assets = [
|
|
'/offline.html',
|
|
'/css/app.css',
|
|
'/css/normalize.css',
|
|
'/css/webapp.css',
|
|
'/css/webappdark.css',
|
|
'/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'
|
|
];
|
|
|
|
// install event
|
|
self.addEventListener('install', evt => {
|
|
console.log('service worker installed');
|
|
evt.waitUntil(
|
|
caches.open(staticCacheName).then((cache) => {
|
|
//console.log('caching shell assets');
|
|
return cache.addAll(assets);
|
|
}).catch((r) => {
|
|
console.log("Install Fail");
|
|
})
|
|
);
|
|
});
|
|
|
|
// activate event
|
|
self.addEventListener('activate', evt => {
|
|
console.log('service worker activated');
|
|
evt.waitUntil(
|
|
caches.keys().then(keys => {
|
|
//console.log(keys);
|
|
return Promise.all(keys
|
|
.filter(key => key !== staticCacheName && key !== dynamicCacheName)
|
|
.map(key => caches.delete(key))
|
|
);
|
|
})
|
|
);
|
|
|
|
});
|
|
|
|
self.addEventListener('fetch', evt => {
|
|
console.log('fetch event');
|
|
|
|
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 => {
|
|
return caches.open(dynamicCacheName).then(cache => {
|
|
cache.put(evt.request.url, fetchRes.clone());
|
|
return fetchRes;
|
|
})
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('push', function(event) {
|
|
if (!(self.Notification && self.Notification.permission === 'granted')) {
|
|
//notifications aren't supported or permission not granted!
|
|
return;
|
|
}
|
|
|
|
if (event.data) {
|
|
let msg = event.data.json();
|
|
console.log(msg)
|
|
event.waitUntil(self.registration.showNotification(msg.title, {
|
|
body: msg.body,
|
|
icon: msg.icon,
|
|
badge: msg.badge,
|
|
actions: msg.actions
|
|
}));
|
|
}
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
if (event.action === 'some_action') {
|
|
self.clients.openWindow('/home');
|
|
} else {
|
|
self.clients.openWindow('/')
|
|
}
|
|
});
|