What is the difference between a "classic" and a "modular" web worker?

I am learning JavaScript Web Worker APIs using the documentation in the Mozilla Developer Network (MDN) as the main source. The documentation suggests that the constructor for the new Worker accepts the type parameter. This type parameter can be either classic or module , according to the same document.

Unfortunately, the documentation does not describe the difference between classic and module . When do I want to use classic vs. module and what behavioral differences exist between the two "types" of Workers?

+7
javascript web-worker
source share
1 answer

The module type serves roughly the same purpose as the type="module" attribute for the script tag . It tells the browser that the working script that loads is the ES6 module (which is the necessary metadata to know how to parse it and run it, how this article goes in bit ).

You would use it if your work module were an ES6 module (possibly with import statements). It also has a bonus to be able to download an employee from another source, if CORS permits that classical workers cannot (which can be an attractive feature even if you do not use import statements).

From HTML Living Standard - Using the JavaScript module as work :

All of our examples are so far shown to employees who run classic scripts. Instead, workers can be created using module scripts, which have the usual advantages: the ability to use the JavaScript import operator to import other modules; strict strict mode by default; and top-level ads that do not pollute the global area of ​​the employee.

Please note that such module-based workers are subject to various restrictions on cross-content compared to classic workers. Unlike classic workers, module workers can be created using the cross-original script if this script is open using the CORS protocol. In addition, the importScripts () method will automatically fail inside working modules; JavaScript import statement is usually the best choice.

You probably won't use it in production today, as browser support for ES6 modules is not so good.

+5
source share

All Articles