82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
const staticCacheName = 'site-static-v2';
|
|
const dynamicCacheName = 'site-dynamic-v1';
|
|
const assets = [
|
|
'/',
|
|
'/css/app.css',
|
|
'/css/normalize.css',
|
|
'/css/webapp.css',
|
|
'/css/webappdark.css',
|
|
'/home',
|
|
'/events'
|
|
];
|
|
|
|
// 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);
|
|
})
|
|
);
|
|
});
|
|
|
|
// 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))
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
// fetch event
|
|
self.addEventListener('fetch', evt => {
|
|
//console.log('fetch event', evt);
|
|
evt.respondWith(
|
|
caches.match(evt.request).then(cacheRes => {
|
|
return cacheRes || fetch(evt.request).then(fetchRes => {
|
|
return caches.open(dynamicCacheName).then(cache => {
|
|
cache.put(evt.request.url, fetchRes.clone()).then(r => {
|
|
return fetchRes;
|
|
});
|
|
|
|
})
|
|
});
|
|
})
|
|
);
|
|
});
|
|
|
|
|
|
//Push notification, this is more of a demo you can test in the dev tool
|
|
self.addEventListener('push', evt => {
|
|
const title = "Test Notification";
|
|
const body = "Det her et en demo på hvordan en notification kan se ud! ";
|
|
const icon = "/icons/browserFavIcon.svg"
|
|
const tag = "simple-push-example-tag";
|
|
evt.waitUntil(
|
|
self.registration.showNotification(title, {
|
|
body : body,
|
|
icon : icon,
|
|
tag : tag
|
|
})
|
|
);
|
|
});
|
|
|
|
|
|
|
|
function displayNotification(){
|
|
if(Notification.permission === 'granted'){
|
|
navigator.serviceWorker.getRegistration()
|
|
.then(function (reg){
|
|
reg.showNotification('Helloe ')
|
|
})
|
|
}
|
|
}
|
|
|