javascript - Use ESM in a service worker file (importexport) - Stack Overflow

I am trying to use a service worker file which is also an ESM module.The register method has an extra a

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:

.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?

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/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?

Share Improve this question asked Jul 9, 2019 at 13:24 Tomáš HübelbauerTomáš Hübelbauer 10.8k17 gold badges74 silver badges141 bronze badges 3
  • 1 .mjs is a Node.js thing, not an ESM thing outside Node.js. (And Node.js now has type. Hopefully .mjs will die a death...) – T.J. Crowder Commented Jul 9, 2019 at 13:27
  • What version of Chrome? Do the contents of your service worker script make any difference? – T.J. Crowder Commented Jul 9, 2019 at 13:28
  • Duplicate of stackoverflow./questions/44118600 – Tobias Buschor Commented Apr 12, 2021 at 7:09
Add a ment  | 

2 Answers 2

Reset to default 5

This is dumb! Chrome will print just DOMException into the console (not even expansible) when logging the error object and Object.keys on that object instance returns [], but when I specifically print e.message the culprit is revealed:

type 'module' in RegistrationOptions is not implemented yet.See https://crbug./824647 for details.

Not amused by Chrome.

On browsers, bare names can't be used to identify modules unless you also have a module map (aka import map, this link has much more info) that maps the bare name to a module.

If worker.js is in the same location as the page loading it, then:

navigator.serviceWorker.register('./worker.js', { type: 'module' });
// -------------------------------^^

Or of course, add a module map.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745183838a4615544.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信