awesome-wm-widgets/assets/js/service-worker.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-09-25 20:38:46 +02:00
importScripts('cache-polyfill.js');
2018-09-20 01:59:07 +02:00
const CACHE_NAME = 'awmw-cache';
const PRE_CACHED_ASSETS = [
2018-09-25 20:47:36 +02:00
'/awesome-wm-widgets/assets/css/materialize.min.css',
'/awesome-wm-widgets/assets/css/style.css',
'/awesome-wm-widgets/assets/css/syntax1.css',
'/awesome-wm-widgets/assets/js/materialize.min.js',
'/awesome-wm-widgets/assets/js/particles.min.js',
'/awesome-wm-widgets/assets/js/init.js'
2018-09-20 01:59:07 +02:00
];
2018-09-25 20:33:42 +02:00
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE_NAME).then(cache => {
2018-09-25 20:55:45 +02:00
return cache.addAll(PRE_CACHED_ASSETS).then(() => self.skipWaiting());
2018-09-25 20:33:42 +02:00
})
);
2018-09-20 01:59:07 +02:00
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
// delete old caches
cacheNames.map(function(cacheName) {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
2018-09-25 20:23:11 +02:00
// self.addEventListener('fetch', function(event) {
// if (event.request.headers.get('accept').startsWith('text/html')) {
// event.respondWith(
// fetch(event.request).catch(error => {
// return caches.match('index.html');
// })
// );
// }
// });
2018-09-25 20:33:42 +02:00
self.addEventListener('fetch', event => {
event.respondWith(
caches.open(CACHE_NAME)
.then(cache => cache.match(event.request, {ignoreSearch: true}))
.then(response => {
return response || fetch(event.request);
})
);
2018-09-20 01:59:07 +02:00
});