From 43033e789c4e781507cef02e19a5472e23215df1 Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 22 Nov 2020 21:25:10 +0000 Subject: [PATCH 1/3] Update sw.js Since the use case is downloading a resource with ServiceWorker and the bug is only observable at Chromium and Chrome we can perform the streaming operation at main thread, transfer the ReadableStream to ServiceWorker only when the stream completes without being cancelled by the user, we do not need the ServiceWorker for anything else before or after the ReadableStream is closed or cancelled. --- sw-stream-cancellation/sw.js | 73 +++++++++++++----------------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/sw-stream-cancellation/sw.js b/sw-stream-cancellation/sw.js index 6084020..de16396 100644 --- a/sw-stream-cancellation/sw.js +++ b/sw-stream-cancellation/sw.js @@ -1,52 +1,33 @@ -self.addEventListener('install', event => { - console.log('[SW] install event fired') -}); +let readable; -self.addEventListener('activate', event => { - console.log('[SW] activate event fired') - event.waitUntil(clients.claim()) +self.addEventListener('message', async (event) => { + readable = event.data; + console.log(event); + event.source.postMessage('ServiceWorker ready to serve download'); }); self.addEventListener('fetch', (event) => { - let url = new URL(event.request.url) - if (url.pathname === '/get-file') { - let interval - let counter = 0 - let cancelNotification = false - let initialCancelReason = false - let rs = new ReadableStream({ - start(c) { - interval = setInterval( () => { - if (counter > 10) { - console.log('[SW] FAIL: Stream must be already closed, but still not') - clearInterval(interval) - c.close() - return - } - console.log('[SW] Stream: enqueue chunk', counter) - try { - c.enqueue( new Uint8Array([counter++]) ) - } catch(e) { - if (cancelNotification) { - console.log('[SW] OK: Stream has been closed with prior notification about cancellation') - if (!initialCancelReason) - console.log('[SW] WARN: Stream cancellation reason is not matched to initial') - } - else - console.log('[SW] FAIL: Stream has been closed without prior notification about cancellation') - clearInterval(interval) - } - }, 2000) - }, - cancel(reason) { - console.log('[SW] Stream cancelled with reason:', reason) - if (reason === url.pathname) - initialCancelReason = true - cancelNotification = true - } + console.log(event); + let url = new URL(event.request.url); + if (url.pathname.includes('get-file')) { + console.log({ readable }); + const headers = { + 'content-disposition': 'attachment; filename="filename.txt"', + }; + event.respondWith( + new Response(readable, { + headers, }) - let r = new Response(rs) - event.respondWith( Promise.resolve(r) ) - } else - event.respondWith( fetch(event.request) ) + ); + } +}); + +self.addEventListener('install', (event) => { + console.log(event); + event.waitUntil(self.skipWaiting()); +}); + +self.addEventListener('activate', (event) => { + console.log(event); + event.waitUntil(self.clients.claim()); }); From b4277f1f4297246721b1d68d3ea06893c28fdc4d Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 22 Nov 2020 21:26:26 +0000 Subject: [PATCH 2/3] Update index.html --- sw-stream-cancellation/index.html | 211 ++++++++++++++++++++++++++---- 1 file changed, 186 insertions(+), 25 deletions(-) diff --git a/sw-stream-cancellation/index.html b/sw-stream-cancellation/index.html index 8f6a8bf..4ec8c8f 100644 --- a/sw-stream-cancellation/index.html +++ b/sw-stream-cancellation/index.html @@ -1,42 +1,203 @@ - - ServiceWorker - Response.body cancellation test + + ServiceWorker - Response.body cancellation test - - + +

Test progress is outputted to console

- Related chromium issue: 638494 - Response.body cancellation should be notified to the service worker.
- View source on GitHub + Related chromium issue: + 638494 + - Response.body cancellation should be notified to the service worker. +
+ View source + on GitHub +

+ + From 659ede44549cfea99d2e9d5deabfbcf14a68e3a4 Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 22 Nov 2020 21:36:36 +0000 Subject: [PATCH 3/3] Update index.html --- sw-stream-cancellation/index.html | 91 ------------------------------- 1 file changed, 91 deletions(-) diff --git a/sw-stream-cancellation/index.html b/sw-stream-cancellation/index.html index 4ec8c8f..2255c72 100644 --- a/sw-stream-cancellation/index.html +++ b/sw-stream-cancellation/index.html @@ -3,98 +3,7 @@ ServiceWorker - Response.body cancellation test - -

Test progress is outputted to console

Related chromium issue: