MV3: creating `new Worker` from the service worker?

518 views
Skip to first unread message

Juraj M.

unread,
Apr 11, 2022, 3:35:43 PM4/11/22
to Chromium Extensions
I have some compression + encoding job which can take 10+ seconds, so in MV2 I've used`new Worker(...)` to prevent freezing of my extension.
But it seems that Service worker doesn't have Worker: "ReferenceError: Worker is not defined".
What can I do?

Robbi

unread,
Apr 12, 2022, 9:29:34 AM4/12/22
to Chromium Extensions, juraj....@gmail.com
The service worker is a worker, so you will simply have to do these jobs within the SW.
If the job request starts from an extension page, you will have to send a message from that page to the SW requiring the execution of the jobs

Juraj M.

unread,
Apr 12, 2022, 9:40:02 AM4/12/22
to Chromium Extensions, Robbi, Juraj M.
Thanks Robbi,
I guess I have no other choice now :(
It was already running in the background script, but having it in a worker would prevent background script from being stuck while the operation was running. Now when it starts, no other background operation can run.
I wish I could have more than one service worker. Or some quick WebAssembly versions of those slow libraries I use :).
Anyway, thanks!

wOxxOm

unread,
Apr 12, 2022, 10:15:08 AM4/12/22
to Chromium Extensions, juraj....@gmail.com, Robbi
MV3 has broken this feature because service worker specification forbids starting a new nested worker, reported in https://crbug.com/1219164.

Doing such heavy encoding without creating a nested worker is a very bad idea because it'll freeze the service worker for the duration of the job, so it won't be able to react to `chrome` events/messages.

Until this bug is fixed, your options are:
  • keep using MV2;
  • open a new tab/window/popup and start the worker inside;
  • add an extension iframe via web_accessible_resources to any web page, start the worker inside;
  • a future workaround would be to use an offscreen document when and if it's implemented.

Juraj M.

unread,
Apr 12, 2022, 10:28:10 AM4/12/22
to Chromium Extensions, wOxxOm, Juraj M.
Thanks!
Those are some crazy out of box thinking ideas! I have a mixed feelings about it.
I do hope the linked bug will be fixed, that's for sure the best solution. Thank you for sharing it!
Reply all
Reply to author
Forward
0 new messages