Revise our fetch to be more in line with W3C spec

This commit is contained in:
Gigiaj 2025-06-10 17:39:45 -05:00
parent f100279e65
commit 07e15e2421

View file

@ -76,13 +76,21 @@ self.addEventListener('fetch', (event: FetchEvent) => {
} }
event.respondWith( event.respondWith(
(async (): Promise<Response> => { (async (): Promise<Response> => {
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 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)); const response = await fetch(url, fetchConfig(token));
if (!response.ok) throw new Error(`Fetch failed: ${response.statusText}`);
return response; return response;
})() })()
); );
event.waitUntil(
(async function () {
console.log('Ensuring fetch processing completes before worker termination.');
})()
);
}); });
const onPushNotification = async (event: PushEvent) => { const onPushNotification = async (event: PushEvent) => {