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

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-09-20 01:59:07 +02:00
const CACHE_NAME = 'awmw-cache';
const PRE_CACHED_ASSETS = [
'/assets/css/materialize.min.css',
2018-09-20 02:28:21 +02:00
'/assets/css/style.css',
2018-09-20 01:59:07 +02:00
'/assets/css/syntax1.css',
'/assets/js/materialize.min.js',
'/assets/js/particles.min.js',
'/assets/js/init.js',
'/index.html'
];
2018-09-25 20:23:11 +02:00
// self.addEventListener('install', function(event) {
// event.waitUntil(
// caches.open(CACHE_NAME).then(function(cache) {
// let cachePromises = PRE_CACHED_ASSETS.map(function(asset) {
// var url = new URL(asset, location.href);
// var request = new Request(url);
// return fetch(request).then(function(response) {
// return cache.put(asset, response);
// });
// });
// return Promise.all(cachePromises);
// })
// );
// });
importScripts('/cache-polyfill.js');
self.addEventListener('install', function(e) {
e.waitUntil(
2018-09-20 01:59:07 +02:00
caches.open(CACHE_NAME).then(function(cache) {
2018-09-25 20:23:11 +02:00
return cache.addAll(PRE_CACHED_ASSETS);
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-20 01:59:07 +02:00
self.addEventListener('fetch', function(event) {
2018-09-25 20:23:11 +02:00
console.log(event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
2018-09-20 01:59:07 +02:00
});