Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

to load a library for a service worker for a PWA but keep getting

DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': 
    The script at 'http://localhost:3000/idb.js' failed to load.

The script file is there, content type is correct, application/javascript, and I've tried it with text/javascript too. My developer tools network tab shows the request starting and then failing really quickly, not getting a chance to hit the server. The status code is (failed) net::ERR_CONNECTION_REFUSED when over http and (failed) net::ERR_FAILED over https. Any help appreciated

According to https://developers.google.com/web/updates/2018/10/tweaks-to-addAll-importScripts

Prior to Chrome 71, calling importScripts() asynchronously outside of the install handler would work. Starting with Chrome 71, those calls throw a runtime exception (unless the same URL was previously imported in an install handler), matching the behavior in other browsers.

I have to move importScripts to the top level of my file or my install handler and it works

Based upon the link you provided, you are not running under HTTPS. You need a site under HTTPS to use a service worker.

My original post wrong. @kmanzana pointed out that localhost is considered a "secure origin".

"Service workers are only available to "secure origins" (HTTPS sites, basically)...localhost is also considered a secure origin" from chromium.org/blink/serviceworker/service-worker-faq – kmanzana Aug 13, 2019 at 16:49 I just checked to make sure and served this file over ngrok with https and I'm still seeing the same problem – kmanzana Aug 13, 2019 at 16:56 Well TIL. What is the status code you receive when you fetch? Are you able to see the response when you directly navigate? – Justin Collins Aug 13, 2019 at 16:56 actually, status code was (failed) net::ERR_CONNECTION_REFUSED when over http and over https it is (failed) net::ERR_FAILED. I'm able to see the response with navigation or curl yes – kmanzana Aug 13, 2019 at 17:00 Well I apologize. I attempted to recreate locally with both HTTPS and HTTP but I was unsuccessful. – Justin Collins Aug 13, 2019 at 17:21

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.