Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web worker importScripts DOMException script failed to load

I'm trying to use importScripts

self.addEventListener('fetch', event => {
  ...
  self.importScripts('idb.js');
  ...
}

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

like image 448
kmanzana Avatar asked Sep 02 '26 02:09

kmanzana


1 Answers

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

like image 103
kmanzana Avatar answered Sep 04 '26 16:09

kmanzana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!