diff --git a/lib/__tests__/sw-push-delivery.test.ts b/lib/__tests__/sw-push-delivery.test.ts index d2d00f7a4..2f5d8c5b5 100644 --- a/lib/__tests__/sw-push-delivery.test.ts +++ b/lib/__tests__/sw-push-delivery.test.ts @@ -187,4 +187,26 @@ describe('which login a push is for', () => { }); }); +describe('when building the notification goes wrong', () => { + it('still shows one', async () => { + const { push, shade } = loadWorker(() => ({ unreadTotal: 1, slot: 0, login: 'fp', email: mail('e1') }), { + globals: { Notification: { get maxActions() { throw new Error('no such thing'); } } }, + }); + await push(delivery('a', 'e1')); + expect(shade.size).toBe(1); + }); + + it('shows one for a push that is no JSON at all', async () => { + const { dispatch, shade } = loadWorker(() => null); + await dispatch('push', { data: { json: () => { throw new SyntaxError('not JSON'); } } }); + expect(shade.size).toBe(1); + }); + + it('shows one for a push without any data', async () => { + const { dispatch, shade } = loadWorker(() => null); + await dispatch('push', {}); + expect(shade.size).toBe(1); + }); +}); + // -- end of cases -- diff --git a/public/sw.js b/public/sw.js index 14d135af6..b8e848cc7 100644 --- a/public/sw.js +++ b/public/sw.js @@ -604,7 +604,19 @@ async function handlePush(event) { ? [] : Object.keys(changed).filter((id) => changed[id] && typeof changed[id] === "object" && "EmailDelivery" in changed[id]); for (const accountId of withNewMail.length > 0 ? withNewMail : [named]) { - await notifyAccount(accountId, emailIds, accountLabel); + try { + await notifyAccount(accountId, emailIds, accountLabel); + } catch (_) { + // Nothing above may cost the user the alert, or a browser its quota for + // this site: see showWithoutAlert. + await self.registration.showNotification(accountLabel ? `New mail (${accountLabel})` : "New mail", { + body: "You have new mail", + tag: "bulwark-mail:" + (accountId || "default"), + icon: `${BASE_PATH}/api/pwa-icon/192`, + badge: `${BASE_PATH}/api/pwa-icon/192`, + data: { kind: "mail-list", accountId }, + }); + } } }