Skip to content

Commit

Permalink
add logging for service worker fails
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg authored Oct 24, 2024
1 parent 40492ac commit d73ff19
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions ui/site/src/serviceWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { url as assetUrl, jsModule } from './asset';
import { log } from 'common/permalog';
import { storage } from 'common/storage';

export default async function () {
Expand All @@ -8,38 +9,38 @@ export default async function () {
self.location.href,
);
workerUrl.searchParams.set('asset-url', document.body.getAttribute('data-asset-url')!);
const reg = await navigator.serviceWorker.register(workerUrl.href, {
scope: '/',
updateViaCache: 'all',
});
const reg =
(await navigator.serviceWorker.getRegistration().then(reg => reg?.update().then(() => reg))) ??
(await navigator.serviceWorker.register(workerUrl.href, { scope: '/', updateViaCache: 'all' }));

const store = storage.make('push-subscribed');
const vapid = document.body.getAttribute('data-vapid');
if (vapid && Notification.permission == 'granted') {
const sub = await reg.pushManager.getSubscription();
const resub = parseInt(store.get() || '0', 10) + 43200000 < Date.now(); // 12 hours
const applicationServerKey = Uint8Array.from(atob(vapid), c => c.charCodeAt(0));
if (!sub || resub) {
const newSub = await reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey,
});
try {
const res = await fetch('/push/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newSub),
});
if (res.ok && !res.redirected) store.set('' + Date.now());
else newSub.unsubscribe();
} catch (err: any) {
console.log('push subscribe failed', err.message);
newSub?.unsubscribe();
}
}
} else {
if (!vapid || Notification.permission !== 'granted') {
store.remove();
(await reg.pushManager.getSubscription())?.unsubscribe();
reg.pushManager.getSubscription().then(sub => sub?.unsubscribe());
return;
}
const sub = await reg.pushManager.getSubscription();
const resub = parseInt(store.get() || '0', 10) + 43200000 < Date.now(); // 12 hours
const applicationServerKey = Uint8Array.from(atob(vapid), c => c.charCodeAt(0));

if (sub && !resub) return;
let newSub: PushSubscription | undefined = undefined;
try {
newSub = await reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey,
});
if (!newSub) throw new Error(!!reg && JSON.stringify(await reg.pushManager.permissionState()));
const res = await fetch('/push/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newSub),
});
if (res.ok && !res.redirected) store.set('' + Date.now());
else throw new Error(res.statusText);
} catch (err: any) {
log('push subscribe failed', err.message, newSub);
newSub?.unsubscribe();
}
}

0 comments on commit d73ff19

Please sign in to comment.