var VERSION = 'v51 - 2019-12-26'; var toCache = [ '/', 'activePage.js', 'activePage.js.map', 'config.js', 'config.js.map', 'favicon.ico', 'hesla.js', 'hesla.js.map', 'hesla.webapp', 'icon-128.png', 'icon-30.png', 'icon-60.png', 'index_de.html', 'index.html', 'require.js', 'screen.css' ]; self.addEventListener('install', event => { console.log('install version = ' + VERSION); event.waitUntil( caches.open(VERSION) .then( c => c.addAll(toCache) .then(v => console.log('Yay!' + VERSION), e => console.log('Error')) ) ); }); self.addEventListener('activate', event => { console.log('activate version = ' + VERSION); console.log('keys:\n' + caches.keys()); event.waitUntil(caches.keys().then( keys => Promise.all( keys.filter(key => key != VERSION). map(key => caches.delete(key)) ) ) ); }); self.addEventListener('fetch', function(event) { event.respondWith(caches.match(event.request).then(function(response) { // caches.match() always resolves // but in case of success response will have value if (response !== undefined) { return response; } else { return fetch(event.request).then(function (response) { // response may be used only once // we need to save clone to put one copy in cache // and serve second one let responseClone = response.clone(); caches.open(VERSION).then(function (cache) { cache.put(event.request, responseClone); }); return response; }).catch(function () { return caches.match('/sw-test/gallery/myLittleVader.jpg'); }); } })); });