v1.4.9b Service worker cache works now

This commit is contained in:
2020-11-26 12:44:53 +01:00
parent 5a073b5cb4
commit 6d0cb5333d
4 changed files with 12 additions and 9 deletions
-81
View File
@@ -1,81 +0,0 @@
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 ')
})
}
}
-28
View File
@@ -1,28 +0,0 @@
//Service Worker Registration
/*
Make sure that we support service worker :)
*/
if('serviceWorker' in navigator){
window.addEventListener('load', () => {
navigator.serviceWorker
.register('/js/serviceWorkerCachedWebpags.js')
.then(reg => console.log("Service Worker has been: registered"))
.catch(err => console.log(`Service Worker error: ${err}`))
})
}
/*
Notification.requestPermission().then(function(permission){
console.log('Notification permission was:', permission)
});
if('caches' in window){
console.log("The browser supports caching ")
}else {
console.log("This browser is some junk, it dosen't support caching :(")
}
*/