const cacheName = 'vis3d-pwa-1', FILES_TO_CACHE = [ '/index.html', '/empty.txt' ]; self.addEventListener('install', function (e) { e.waitUntil( caches.open(cacheName).then(function (cache) { return cache.addAll(FILES_TO_CACHE); }) ); }); self.addEventListener('fetch', function (event) { if (event.request.method !== 'GET') return; event.respondWith(handle.bind(event).call().then(r => { return r })) }); async function handle() { let event = this, url = event.request.url; if (url === self.registration.scope) { url += 'index.html'; } const cache = await caches.open(cacheName); let response = await fetch(url).then((r) => { return r }).catch(async err => { let cachedResponse = await cache.match(url); if (cachedResponse) { return cachedResponse; } cachedResponse = await cache.match('/empty.txt'); return cachedResponse; }); if (response) { await cache.put(event.request, response.clone()); return response; } }