I am trying to use a service worker file which is also an ESM module.
The register
method has an extra argument accepting an options
object which has a type
field whose valid values seem to be classic
and module
, but when I use:
navigator.serviceWorker.register('worker.js', { type: 'module' });
// `worker.mjs` doesn't work either
// The file exists in both cases!
I get an unspecified DOMException
with no message in Chrome.
I figured what the valid values for type
were by reading the spec, specifically this:
https://html.spec.whatwg.org/multipage/workers.html#workertype
It seems to me like my code is valid.
As a sanity check, I also tried to explicity set type
to classic
and the service worker registration then goes through fine. If I set it to an invalid value, I get a TypeError
telling me so, so it's not like the browser is not yet aware of type: module
. It is treated as a special case, it just throws a DOMException
with no message.
Am I using the type
field correctly? Is it still too early and it is not supported in browsers?