Просмотр исходного кода

fase6(app): timeout de 15s en pushManager.subscribe

Si el push service del navegador no responde (sin red, FCM inalcanzable),
el boton no se queda en 'Activando...' indefinidamente: salta el error
generico de la tarjeta.
agent 4 месяцев назад
Родитель
Сommit
ff0492bc15
2 измененных файлов с 12 добавлено и 4 удалено
  1. 1 0
      app/dev-dist/registerSW.js
  2. 11 4
      app/src/pages/Settings.tsx

+ 1 - 0
app/dev-dist/registerSW.js

@@ -0,0 +1 @@
+if('serviceWorker' in navigator) navigator.serviceWorker.register('/dev-sw.js?dev-sw', { scope: '/', type: 'module' })

+ 11 - 4
app/src/pages/Settings.tsx

@@ -1116,10 +1116,17 @@ function PushNotificationsCard({ reduce, onSaved }: { reduce: boolean; onSaved:
         return
       }
       const reg = await navigator.serviceWorker.ready
-      const sub = await reg.pushManager.subscribe({
-        userVisibleOnly: true,
-        applicationServerKey: urlBase64ToUint8Array(vapid) as BufferSource,
-      })
+      // Timeout de seguridad: si el push service del navegador no responde
+      // (sin red, FCM inalcanzable…) el botón no se queda «Activando…»
+      const sub = await Promise.race([
+        reg.pushManager.subscribe({
+          userVisibleOnly: true,
+          applicationServerKey: urlBase64ToUint8Array(vapid) as BufferSource,
+        }),
+        new Promise<never>((_, reject) =>
+          window.setTimeout(() => reject(new Error('push subscribe timeout')), 15000),
+        ),
+      ])
       const json = sub.toJSON()
       const ok = await postPushSubscribe({
         endpoint: sub.endpoint,