Ekapp/skolehjem/public/serviceWorkerCachedWebpags.js

143 lines
4.1 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 => {
//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) => {
console.log("I dont workkkkk");
})
);
});
// 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
2020-11-27 10:27:21 +00:00
/*
self.addEventListener('fetch', evt => {
2020-11-26 11:44:53 +00:00
console.log('fetch event');
2020-11-27 10:27:21 +00:00
evt.respondWith(
caches.match(evt.request).then(async function (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;
});
}).catch((r) => {
console.log("I dont work");
});
});
})
);
});*/
self.addEventListener('fetch', evt => {
console.log('fetch event', evt);
evt.respondWith(
2020-11-27 10:27:21 +00:00
caches.match(evt.request).then(cacheRes => {
return cacheRes || 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-27 10:27:21 +00:00
}).catch(function () {
return caches.match('/offline.html');
})
);
});
2020-11-27 10:27:21 +00:00
/*
self.addEventListener('fetch', evt => {
console.log("ARGHHHH");
if(navigator.onLine) {
evt.respondWith(async function() {
return fetch(evt.request).then((fetchRes) => {
caches.open(dynamicCacheName).then(cache => {
cache.put(evt.request.url, fetchRes.clone());
})
})
});
} else {
console.log('fetch event');
evt.respondWith(async function () {
let cachedResponse = await caches.match(evt.request);
if(cachedResponse) return cachedResponse;
else return caches.match("/offline.html");
});
}
});*/
2020-11-26 11:44:53 +00:00
/*
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 ')
})
}
}
2020-11-26 11:44:53 +00:00
*/