ServiceWorker streaming demo

This shows how you can leverage a ServiceWorker to stream without ever having to store a full Blob of the ZIP in the browser.

I'm doing it here by POSTing a simple form with an arbitrary number of URL fields (therefore no JavaScript is needed to send the list — no onsubmit — but only to register the ServiceWorker and clone the URL input). You could also use hidden fields to specify some extra URLs or headers, even specify all the URLs that way to make a non-interactive form with only a submit button visible.

The ServiceWorker (just 30 lines) intercepts the request, lazily fetches the URLs found in the form data (all the fields named "url") with a generator, feeds that to downloadZip and responds with downloadZip's return value (which is a Response). That's it. The process is only a bit more complicated that using a Blob (look at the code).

Until Safari Technology Preview 138, an old bug in Safari bypassed the ServiceWorker as soon as the browser detected that it should download the content, whether through a form or a download link. It's hard to detect this bug with JavaScript (fetching the zip works as expected), so you'll need to do browser detection 🤦‍♂️ and fall back to the less efficient Blob method when it's Safari.

URLs of documents to include in the ZIP (for cross-origin URLs, the server must send CORS headers):