cc young
cc young

Reputation: 20203

can a Web Workers file handle includes or modules?

I have several web workers, each supporting different tables in the database. Each of web worker uses a core set of functions (for accessing the server, querying, etc).

What is the best means for putting these shared, common fuctions into the several web workers?

it seems that if they are loaded as files, then the browser will have them cached and the workers themselves can be lighter.

perhaps they could be loaded as es6 modules.

Upvotes: 3

Views: 1128

Answers (2)

Tomáš Zato
Tomáš Zato

Reputation: 53119

Also note that RequireJS works in web workers so if you use that for your app, you can easily have dependencies resolved.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074028

Web workers receive a global function, importScripts (spec | MDN), that you can use to import scripts into the worker's global context:

importScripts('shared.js');

Someday, I'm sure we'll be able to use ES2015 (ES6) modules, but that day will likely be at least a year or two from now. :-)

Upvotes: 1

Related Questions