Specifying Module Service Worker In manifest.json?

412 views
Skip to first unread message

PhistucK

unread,
Mar 13, 2021, 1:41:23 PM3/13/21
to Simeon Vincent, Chromium-extensions
Given the upcoming module service worker support (to be approved, but not reason it would not be), how could this be specified in manifest.json for the background service worker?
Or will it just be module based by (unchangeable) default? 

PhistucK

wOxxOm

unread,
Mar 14, 2021, 7:50:06 AM3/14/21
to Chromium Extensions, PhistucK, Simeon Vincent
I wonder if module mode can be used by default and be the only mode? Some extensions will need fixing, arguably, judging by the following list of differences that apply to extension service worker hand-picked from https://usefulangle.com/post/256/script-type-module-vs-script-javascript.

> Module script executes in 'use strict' mode

Arguably, a good change all around, in line with MV3 secure-by-default theme. I guess some extensions were unknowingly relying on quirks of the ancient sloppy mode so switching to the strict mode might help them improve the code.

> Module script has its own scope

A global variable `foo` won't be automatically available as `self.foo` or `globalThis.foo` or `this.foo` (when `this` is global). This will be a breaking change for some extensions but fixing it should be trivial.

> Module script can import other modules

This is what modules are about. Both the static and dynamic imports are more flexible and arguably work faster than the importScripts() as the latter needs internal "hacks" to pause the entire JS environment, AFAIK.

> Module script has top-level `this` as `undefined`

This may be a breaking change for some extensions or libraries, but it was probably wrong to rely on it in the first place.

wOxxOm

unread,
Apr 9, 2021, 1:17:52 AM4/9/21
to Chromium Extensions, PhistucK, Simeon Vincent
Type will be added to background key in the manifest file, see https://crbug.com/1194681:

"background": {
  "service_worker": "sw.js",
  "type": "module"
}

On Saturday, March 13, 2021 at 9:41:23 PM UTC+3 PhistucK wrote:

PhistucK

unread,
Apr 9, 2021, 10:19:36 AM4/9/21
to wOxxOm, Chromium Extensions, Simeon Vincent
Thank you for the update!

PhistucK

Simeon Vincent

unread,
Apr 14, 2021, 5:54:41 AM4/14/21
to PhistucK, wOxxOm, Chromium Extensions
For a little more background here, we decided not to change the default type to "module" in order to align with the web platform. Principle of least astonishment and all that. I expect that once this lands in stable I will update our canonical examples and guides to lean heavily into modules as the preferred pattern.

Simeon - @dotproto
Chrome Extensions DevRel

Reply all
Reply to author
Forward
0 new messages