8

If I try to use web workers through a JavaScript file, Chrome throws an error -

Uncaught SecurityError: Failed to create a worker: script at '(path)/worker.js' cannot be accessed from origin 'null'.

But it allows them if we use directly through the HTML.

The answer on Chrome can't load web worker says Chrome doesn't let you load web workers when running scripts from a local file.

Why doesn't chrome allow web workers to run locally?

Web Workers work completely fine in Firefox, Safari and in Edge

Community
  • 1
  • 1
Inderpartap Cheema
  • 463
  • 1
  • 7
  • 17
  • 2
    web workers have been supported by chrome since version 4.00, perhaps share your code? I suspect it's access to `worker.js` that's the problem. – Jim Jun 09 '16 at 06:58
  • Are your application running http (http://, https://) or streight from the file system (file://)? – Morten Olsen Jun 09 '16 at 07:00

2 Answers2

13

This question was already asked. The workers should work in HTML files opened from disk as long as you use relative path. However, if chrome implements this correctly has been disputed.

I advise that you try to use relative path in your scripts:

new Worker("./scripts/worker.js");

If that doesn't work, see this workaround: https://stackoverflow.com/a/33432215/607407

Specifically, load worker as a function, then convert the function to string:

function worker_function() {
    // all worker code here
}
var worker = new Worker(URL.createObjectURL(new Blob(["("+worker_function.toString()+")()"], {type: 'text/javascript'})));
Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 3
    The answer you mentioned says "Chrome doesn't let you load web workers when running scripts from a local file." This was my actual question. Why can't we use workers locally in Chrome? – Inderpartap Cheema Jun 09 '16 at 18:18
  • if the worker js file is in the same dir I just need to call it like this correct? `new Worker("worker.js");` – utdev Aug 08 '17 at 09:57
  • @InderpartapCheema If you're asking *Why*, then it's off topic, see help center. – Tomáš Zato Aug 29 '17 at 13:07
  • 4
    It's on topic and I also am curious about this + the more questions the merrier- I for one am googling this in 2018 and am frustrated by the sheer absence of identical such questions as by now I expect there to be dozens of answers for this kind of thing , each shining their own hint of wisdom on the subject. – Vasily Hall Oct 21 '18 at 06:33
  • 2
    @VasilyHall I understand your curiosity and frustration. But asking "why" did Google Chrome team make decision about a feature is not on topic on stackoverflow: It would be pure speculation - unless one of the decision makers actually weighs in. – Tomáš Zato Oct 21 '18 at 20:20
  • Fair enough, maybe in years ahead some engineers would pop on this thread and anonymously spill the secret. – Vasily Hall Oct 21 '18 at 20:33
  • On another note, I'm excited to try your solution from another post - about doing a quick internally-external script via Blob object! – Vasily Hall Oct 24 '18 at 15:27
  • The relative URL didn't work in any of my browsers, but the Blob-way worked. – agiopnl Jan 27 '22 at 22:00
2
var cblock=`

function workerFunc(e){

    console.info('Hello World!',e.data.msg)
    postMessage({msg:'Complete!'})
}

addEventListener('message',workerFunc)
`    
var a=new Worker(URL.createObjectURL(new Blob( [cblock], {type:'text/javascript'} )))    
a.onmessage=function(e){ console.info('My worker called!',e.data.msg) }    
a.onerror=function(e){ console.info( JSON.stringify(e,' ',2) ) }    
a.postMessage({ msg:'Chrome WebWorkers work!' }) 

// Hello World! Chrome WebWorkers work!
// My worker called! Complete!