Workaround for posponed importScripts() in the service worker

12 views
Skip to first unread message

Getfree

unread,
1:03 AM (2 hours ago) 1:03 AM
to Chromium Extensions
I'm running into one problem after another with this MV3 nightmare.
Currently, I'm trying to solve the problem of importing a script file into the service worker after the first turn of the event loop.

The importScripts function only works during the first turn of the event loop, but according to comment #11 on this issue https://issues.chromium.org/issues/40760920#comment11 there seems to be a weird trick to make importScripts work AFTER the first turn of the event loop.

Basically, you have to call importScripts twice, once inside the "install" event and a second time once you actually need to execute the script.

self.addEventListener('install', () => {
       importScripts('foo.js'); //this first import is necessary for some reason?
});

(async ()=>{
       await some_promise;
       importScripts('foo.js'); //this second import apparently executes the script
})();

I can't find any mention of this strange feature in MDN.

Could someone please explain how are the two calls to importScripts different?
Does the first call not execute the script file?
If not, then what does it do?

Reply all
Reply to author
Forward
0 new messages