jimmywarting / StreamSaver.js

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

service worker is not getting registered at my standalone web offline without internet(without requesting jimmywarting.github.io) #221

Open LironShirazi opened 3 years ago

LironShirazi commented 3 years ago

hey, I'm trying to use the StreamSaver in standalone host, so I have changed the: streamSaver.mitm = 'myapp.example/mitm.html' for example. The keepAlive function works and sends a ping every 10 seconds, but the service worker is not getting registered in the registerWorker() function, so the function is not being called for some reason and the sw.js is not in use. im using http secure context and not https. is there any solution for me, im stucked for hours, any suggestions will be appreciated Thanks!

jimmywarting commented 3 years ago

http://localhost is a secure context, http://custom-hostname.local is not

You could check if the page is secure by running alert(isSecureContext)

It could also be that third party cookies are disabled - try to enable that

LironShirazi commented 3 years ago

Thanks for your response, Third cookies are not disabled so it's probably be something else, Is it possible to make the call to http://custom-hostname.exmaple or not to https:// I mean, a service worker can get called/created by the prefix of http, because in our closed network we cant use https. ping is sent every 10 seconds it's not downloading the file.

jimmywarting commented 3 years ago

Is it possible to make the call to http://custom-hostname.exmaple or not to https:// I mean, a service worker can get called/created by the prefix of http, because in our closed network we cant use https.

Service worker can't be installed on insecure context. So it will not work. You would have to deal with it in some other way.

If the download is coming from the server (backend) then i recommend to download it using only the server instead of emulating what the server dose with StreamSaver + service worker.

The most simplest way would be to just create a link: <a href="url" download="filename.txt">downlaod</a> and click on that link (manually or hidden from sight with javascript a.click())

The server should respond back with a content-disposition attachment header (And you can't use Ajax) a simple navigation to the download URL is sufficient, or you can use a hidden iframe or submit a <form> if you need to post something and doesn't work with a simple GET

StreamSaver targeted group is for those who generates huge amount of data on the client side (not from a server - only exception is when you are forced to send a request header). It could be downloading something using P2P with WebRTC, record a video from webcam or anything like that


The other solution is that you need to create a Blob, create a link and download it that way. FileSaver.js could be a option

guest271314 commented 3 years ago

One option could be to use ServiceWorker of an MV3 Chromium/Chrome extension. The local HTML page can be loaded at URL chrome-extension:///index.html then communication can proceed the same as HTTPS protocol.