-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
7 lines (7 loc) · 1.67 KB
/
Copy pathsw.js
File metadata and controls
7 lines (7 loc) · 1.67 KB
1
2
3
4
5
6
7
importScripts('/generated/offline-manifest.js');
const manifest = self.VIRELIA_OFFLINE_MANIFEST;
const cacheName = 'virelia-' + manifest.version;
self.addEventListener('install', event => event.waitUntil(caches.open(cacheName).then(cache => cache.addAll(['/','/style.css','/main.js','/offline.js','/manifest.webmanifest']))));
self.addEventListener('activate', event => event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key !== cacheName).map(key => caches.delete(key)))).then(() => self.clients.claim())));
self.addEventListener('fetch', event => event.respondWith(caches.match(event.request).then(hit => hit || fetch(event.request).then(response => { if (response.ok && new URL(event.request.url).origin === location.origin) caches.open(cacheName).then(cache => cache.put(event.request, response.clone())); return response; }))));
self.addEventListener('message', event => { if (event.data?.type !== 'DOWNLOAD_OFFLINE') return; event.waitUntil((async () => { const cache = await caches.open(cacheName); let done = 0; for (const url of manifest.assets) { if (event.data.cancelled) break; try { const request = new Request(url, { mode: url.startsWith('http') ? 'cors' : 'same-origin' }); if (!(await cache.match(request))) { const response = await fetch(request); if (response.ok || response.type === 'opaque') await cache.put(request, response); } } catch (_) {} done++; const clients = await self.clients.matchAll(); clients.forEach(client => client.postMessage({ type: 'OFFLINE_PROGRESS', done, total: manifest.assets.length, bytes: manifest.bytes })); } const clients = await self.clients.matchAll(); clients.forEach(client => client.postMessage({ type: 'OFFLINE_READY' })); })()); });