From 07e15e2421fd076dddf08a7266763a5f88755aae Mon Sep 17 00:00:00 2001 From: Gigiaj Date: Tue, 10 Jun 2025 17:39:45 -0500 Subject: [PATCH] Revise our fetch to be more in line with W3C spec --- src/sw.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sw.ts b/src/sw.ts index c62d560a..3fd25134 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -76,13 +76,21 @@ self.addEventListener('fetch', (event: FetchEvent) => { } event.respondWith( (async (): Promise => { - console.log('Unironic race condition mitigation it seems.'); + if (!event.clientId) throw new Error('Missing clientId'); const client = await self.clients.get(event.clientId); - const token: string = await sendAndWaitForReply(client, 'token', {}); + if (!client) throw new Error('Client not found'); + const token = await sendAndWaitForReply(client, 'token', {}); + if (!token) throw new Error('Failed to retrieve token'); const response = await fetch(url, fetchConfig(token)); + if (!response.ok) throw new Error(`Fetch failed: ${response.statusText}`); return response; })() ); + event.waitUntil( + (async function () { + console.log('Ensuring fetch processing completes before worker termination.'); + })() + ); }); const onPushNotification = async (event: PushEvent) => {