Bernt Christian Egeland 6 месяцев назад
Родитель
Сommit
4c7fed0439
4 измененных файлов с 14 добавлено и 5 удалено
  1. BIN
      public/icons/apple-touch-icon.png
  2. BIN
      public/icons/icon-192.png
  3. BIN
      public/icons/icon-512.png
  4. 14 5
      public/sw.js

BIN
public/icons/apple-touch-icon.png


BIN
public/icons/icon-192.png


BIN
public/icons/icon-512.png


+ 14 - 5
public/sw.js

@@ -1,9 +1,10 @@
 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_URL, '/icons/icon-192.png']))
+    caches.open(CACHE_NAME).then((cache) => cache.addAll(OFFLINE_ASSETS))
   )
   self.skipWaiting()
 })
@@ -18,8 +19,16 @@ self.addEventListener('activate', (e) => {
 })
 
 self.addEventListener('fetch', (e) => {
-  if (e.request.mode !== 'navigate') return
-  e.respondWith(
-    fetch(e.request).catch(() => caches.match(OFFLINE_URL))
-  )
+  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))
+    )
+  }
 })