v1.4.8r Add service worker to cache the webpages, but i dont know if it works
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
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 ')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
//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 :(")
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user