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; }; }