From f7ddb2e7ae30188852dd936f8d721492058e78e0 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 24 Jun 2026 13:57:03 +0530 Subject: [PATCH] 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 --- server/public/home.html | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/server/public/home.html b/server/public/home.html index f53bf29..990bf4a 100644 --- a/server/public/home.html +++ b/server/public/home.html @@ -589,7 +589,7 @@ - +
Loading…
@@ -1453,20 +1453,25 @@ document.addEventListener('click', ensureNotifyPermission, { once:true }); 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{ 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(){ - if(!_swReg || !('PushManager' in window)) return; - if(!('Notification' in window) || Notification.permission!=='granted') return; // only once allowed - let cfg; try{ cfg=await fetch('/api/push/vapid').then(r=>r.json()); }catch(_){ return; } - if(!cfg || !cfg.enabled || !cfg.key) return; // server hasn't configured VAPID -> stay on in-page notifs - let sub=null; try{ sub=await _swReg.pushManager.getSubscription(); }catch(_){} - if(!sub){ try{ sub=await _swReg.pushManager.subscribe({ userVisibleOnly:true, applicationServerKey:urlB64ToUint8(cfg.key) }); }catch(_){ return; } } - try{ await postJSON('/api/push/subscribe', sub.toJSON()); pushActive=true; }catch(_){} + try{ + if(!_swReg){ try{ _swReg=await navigator.serviceWorker.ready; }catch(_){ return; } } + if(!('Notification' in window) || Notification.permission!=='granted'){ console.log('[push] permission not granted ('+(window.Notification?Notification.permission:'n/a')+') — skipping subscribe'); return; } + let cfg; try{ cfg=await fetch('/api/push/vapid').then(r=>r.json()); }catch(e){ console.warn('[push] /api/push/vapid failed:', e); return; } + if(!cfg || !cfg.enabled || !cfg.key){ console.warn('[push] server push disabled (VAPID not set):', cfg); return; } + let sub=await _swReg.pushManager.getSubscription(); + 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 // notification click is not an in-page gesture, so an in-place open won't paint until you