jimmywarting / StreamSaver.js

StreamSaver writes stream to the filesystem directly asynchronous
https://jimmywarting.github.io/StreamSaver.js/example.html
MIT License
3.97k stars 413 forks source link

[Feature Request] Reading sw.js URL from query string for simpler self host #255

Closed yume-chan closed 2 years ago

yume-chan commented 2 years ago

Usually self host StreamSaver means manually (or use copy-webpack-plugin) copy mitm.html and sw.js to public folder and set up StreamSaver.mitm = '/static/mitm.html'.

But with Webpack 5 and URL Assets, one can do something like

StreamSaver.mitm = new URL('streamsaver/mitm.html?url', import.meta.url).toString() + '?sw=' + new URL('streamsaver/sw.js?url', import.meta.url).toString();

And Webpack will automatically copy mitm.html and sw.js to public folder. One bonus point is that these files now have names with hashes so they can be cached forever.

However, the sw.js path is hardcoded so the above code doesn't really work.

https://github.com/jimmywarting/StreamSaver.js/blob/cd8e32a84f951541c5be183b2b409bd17dbb16cd/mitm.html#L41

I have tested locally that some thing like this is possible (although URLSearchParams may have compatibility issue)

return swReg || navigator.serviceWorker.register(new URLSearchParams(location.search).get('sw') || 'sw.js', { scope: './' })
yume-chan commented 2 years ago

Actually service worker script URL shouldn't change. So closing in favor of copying deployment.