From 53e026f8f3ca0beb0ec16b6765d52e18a5e9a549 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 28 Oct 2021 21:06:37 -0400 Subject: [PATCH] Event handlers must respond before dispatching another event --- assets/js/sw/download.ts | 16 ++++++++-------- assets/js/utils/requests.js | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/assets/js/sw/download.ts b/assets/js/sw/download.ts index 466fd835..27a774ed 100644 --- a/assets/js/sw/download.ts +++ b/assets/js/sw/download.ts @@ -15,15 +15,15 @@ export function handleDownload(event: FetchEvent, url: URL): void { return event.respondWith(new Response('Don\'t know what to download!', { status: 400 })); } - const generateResponse = ifOk((upstream: Response) => { - const headers = new Headers(upstream.headers); + const response = + fetch(target) + .then(ifOk((upstream: Response) => { + const headers = new Headers(upstream.headers); - headers.set('content-disposition', `attachment; filename="${escapeFilename(name)}"`); + headers.set('content-disposition', `attachment; filename="${escapeFilename(name)}"`); - return new Response(upstream.body, { headers }); - }); + return new Response(upstream.body, { headers }); + })); - fetch(target) - .then(generateResponse) - .then(event.respondWith); + event.respondWith(response); } diff --git a/assets/js/utils/requests.js b/assets/js/utils/requests.js index 91c9277a..241593f4 100644 --- a/assets/js/utils/requests.js +++ b/assets/js/utils/requests.js @@ -70,7 +70,7 @@ function fetchBackoff(...fetchArgs) { */ function escapeFilename(name) { return name - .replace(/[^-_+a-zA-Z0-9]/, '_') + .replace(/[^.-_+a-zA-Z0-9]/, '_') .substring(0, 150); } @@ -82,7 +82,7 @@ function escapeFilename(name) { */ function ifOk(responseGenerator) { return resp => { - if (resp.ok) return new Response(responseGenerator(resp)); + if (resp.ok) return responseGenerator(resp); return resp; }; }