fix(push): wait for active SW before subscribe + log every step (subscribe was failing silently)
subscribePush() swallowed all errors, so if pushManager.subscribe() failed (e.g. called before the service worker was active) nobody ever subscribed and there was no trace. Now: await serviceWorker.ready before subscribing, and console.log/warn each step so the real failure is visible. Server send path verified independently (web-push builds valid VAPID requests). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+16
-11
@@ -589,7 +589,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script src="/icons.js?v=3"></script>
|
<script src="/icons.js?v=3"></script>
|
||||||
<script>window.__BUILD='2026-06-24-push2';console.log('%cBizGaze Connect','color:#1F3B73;font-weight:bold','build '+window.__BUILD);</script>
|
<script>window.__BUILD='2026-06-24-push3';console.log('%cBizGaze Connect','color:#1F3B73;font-weight:bold','build '+window.__BUILD);</script>
|
||||||
<div class="loading" id="loading">Loading…</div>
|
<div class="loading" id="loading">Loading…</div>
|
||||||
|
|
||||||
<header>
|
<header>
|
||||||
@@ -1453,20 +1453,25 @@ document.addEventListener('click', ensureNotifyPermission, { once:true });
|
|||||||
let pushActive=false, _swReg=null;
|
let pushActive=false, _swReg=null;
|
||||||
function urlB64ToUint8(base64){ const pad='='.repeat((4-base64.length%4)%4); const b=(base64+pad).replace(/-/g,'+').replace(/_/g,'/'); const raw=atob(b); const arr=new Uint8Array(raw.length); for(let i=0;i<raw.length;i++) arr[i]=raw.charCodeAt(i); return arr; }
|
function urlB64ToUint8(base64){ const pad='='.repeat((4-base64.length%4)%4); const b=(base64+pad).replace(/-/g,'+').replace(/_/g,'/'); const raw=atob(b); const arr=new Uint8Array(raw.length); for(let i=0;i<raw.length;i++) arr[i]=raw.charCodeAt(i); return arr; }
|
||||||
async function setupPush(){
|
async function setupPush(){
|
||||||
if(!('serviceWorker' in navigator)) return;
|
if(!('serviceWorker' in navigator) || !('PushManager' in window)){ console.warn('[push] not supported by this browser'); return; }
|
||||||
try{ _swReg=await navigator.serviceWorker.register('/sw.js'); }catch(_){ return; }
|
try{ await navigator.serviceWorker.register('/sw.js'); }catch(e){ console.warn('[push] SW register failed:', e); return; }
|
||||||
|
try{ _swReg=await navigator.serviceWorker.ready; }catch(e){ console.warn('[push] SW never became ready:', e); return; } // ensure an ACTIVE worker before subscribe()
|
||||||
|
console.log('[push] service worker ready');
|
||||||
// Clicking an OS notification asks us (if a tab is already open) to open that chat in place.
|
// Clicking an OS notification asks us (if a tab is already open) to open that chat in place.
|
||||||
try{ navigator.serviceWorker.addEventListener('message',(e)=>{ const d=e.data||{}; if(d.type==='open-chat' && d.id){ try{ selectChat(d.kind||'dm', d.id); }catch(_){} } }); }catch(_){}
|
try{ navigator.serviceWorker.addEventListener('message',(e)=>{ const d=e.data||{}; if(d.type==='open-chat' && d.id){ try{ selectChat(d.kind||'dm', d.id); }catch(_){} } }); }catch(_){}
|
||||||
try{ await subscribePush(); }catch(_){}
|
await subscribePush();
|
||||||
}
|
}
|
||||||
async function subscribePush(){
|
async function subscribePush(){
|
||||||
if(!_swReg || !('PushManager' in window)) return;
|
try{
|
||||||
if(!('Notification' in window) || Notification.permission!=='granted') return; // only once allowed
|
if(!_swReg){ try{ _swReg=await navigator.serviceWorker.ready; }catch(_){ return; } }
|
||||||
let cfg; try{ cfg=await fetch('/api/push/vapid').then(r=>r.json()); }catch(_){ return; }
|
if(!('Notification' in window) || Notification.permission!=='granted'){ console.log('[push] permission not granted ('+(window.Notification?Notification.permission:'n/a')+') — skipping subscribe'); return; }
|
||||||
if(!cfg || !cfg.enabled || !cfg.key) return; // server hasn't configured VAPID -> stay on in-page notifs
|
let cfg; try{ cfg=await fetch('/api/push/vapid').then(r=>r.json()); }catch(e){ console.warn('[push] /api/push/vapid failed:', e); return; }
|
||||||
let sub=null; try{ sub=await _swReg.pushManager.getSubscription(); }catch(_){}
|
if(!cfg || !cfg.enabled || !cfg.key){ console.warn('[push] server push disabled (VAPID not set):', cfg); return; }
|
||||||
if(!sub){ try{ sub=await _swReg.pushManager.subscribe({ userVisibleOnly:true, applicationServerKey:urlB64ToUint8(cfg.key) }); }catch(_){ return; } }
|
let sub=await _swReg.pushManager.getSubscription();
|
||||||
try{ await postJSON('/api/push/subscribe', sub.toJSON()); pushActive=true; }catch(_){}
|
if(!sub){ sub=await _swReg.pushManager.subscribe({ userVisibleOnly:true, applicationServerKey:urlB64ToUint8(cfg.key) }); }
|
||||||
|
await postJSON('/api/push/subscribe', sub.toJSON());
|
||||||
|
pushActive=true; console.log('[push] subscribed OK');
|
||||||
|
}catch(e){ console.warn('[push] subscribe failed:', e); } // surfaced (not swallowed) so we can see the real reason
|
||||||
}
|
}
|
||||||
// Open the chat from an in-page notification. Navigation reliably repaints across browsers (a
|
// Open the chat from an in-page notification. Navigation reliably repaints across browsers (a
|
||||||
// notification click is not an in-page gesture, so an in-place open won't paint until you
|
// notification click is not an in-page gesture, so an in-place open won't paint until you
|
||||||
|
|||||||
Reference in New Issue
Block a user