Ekapp/skolehjem/public/serviceWorkerCachedWebpags.js

105 lines
2.9 KiB
JavaScript
Raw Normal View History

const staticCacheName = 'site-static-v2';
const dynamicCacheName = 'site-dynamic-v1';
const assets = [
2020-11-27 10:27:21 +00:00
'/offline.html',
'/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'
];
// install event
self.addEventListener('install', evt => {
2020-11-30 14:14:11 +00:00
console.log('service worker installed');
evt.waitUntil(
caches.open(staticCacheName).then((cache) => {
2020-11-26 11:44:53 +00:00
//console.log('caching shell assets');
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");
})
);
});
// activate event
self.addEventListener('activate', evt => {
2020-11-30 14:14:11 +00:00
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))
);
})
);
2020-11-30 14:14:11 +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 => {
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-30 20:13:20 +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-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-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('/')
}
});