| 12345678910111213141516171819202122232425262728293031323334 |
- const CACHE_NAME = 'torqvoice-offline-v1'
- const OFFLINE_URL = '/offline.html'
- const OFFLINE_ASSETS = [OFFLINE_URL, '/icons/icon-192.png']
- self.addEventListener('install', (e) => {
- e.waitUntil(
- caches.open(CACHE_NAME).then((cache) => cache.addAll(OFFLINE_ASSETS))
- )
- self.skipWaiting()
- })
- self.addEventListener('activate', (e) => {
- e.waitUntil(
- caches.keys().then((keys) =>
- Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
- )
- )
- self.clients.claim()
- })
- self.addEventListener('fetch', (e) => {
- if (e.request.mode === 'navigate') {
- e.respondWith(
- fetch(e.request).catch(() => caches.match(OFFLINE_URL))
- )
- return
- }
- if (OFFLINE_ASSETS.includes(new URL(e.request.url).pathname)) {
- e.respondWith(
- fetch(e.request).catch(() => caches.match(e.request))
- )
- }
- })
|