// we'll version our cache (and learn how to delete caches in // some other post) let cacheName = 'v1.1::static'; const pageCached = [ '/', '/index.html', '/manifest.json', '/sw.js', 'data/contacts.json', '/tools.ico', '/css/mobiparc.css', '/js/mobiparc.js', '/js/jquery-3.2.1.min.js', '/js/jquery-ui.min.js', '/js/handlebars.min.js', '/img/tools_512.png', '/img/tools_128.png', '/img/tools.svg', ]; self.addEventListener('install', e => { // once the SW is installed, go ahead and fetch the resources // to make this work offline e.waitUntil( caches.open(cacheName).then(cache => { return cache.addAll(pageCached).then(() => self.skipWaiting()); }) ); }); self.addEventListener('activate', function (e) { //console.log('[ServiceWorker] Activate'); e.waitUntil( caches.keys().then(keyList => { return Promise.all(keyList.map(key => { if (key !== cacheName) { console.log('[ServiceWorker] Removing old cache', key); return caches.delete(key); } })); }) ); return self.clients.claim(); }); // when the browser fetches a url, either response with // the cached object or go ahead and fetch the actual url self.addEventListener('fetch', event => { event.respondWith( // ensure we check the *right* cache to match against caches.open(cacheName).then(cache => { return cache.match(event.request).then(res => { return res || fetch(event.request) }); }) ); });