0

I'm learning about web workers from this site and following the example, but I keep getting this error. What does this mean? Both my index.html and pi.js files are saved on my desktop.

index.html:9 Uncaught SecurityError: Failed to construct 'Worker': Script at 'file:///Users/jlei/Desktop/pi.js' cannot be accessed from origin 'null'.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
akantoword
  • 2,824
  • 8
  • 26
  • 43
  • This was asked before several times. [Please use Google](https://www.google.cz/search?q=Script+at++cannot+be+accessed+from+origin+'null'). Particularly I recommend my answer here: http://stackoverflow.com/a/33432215/607407 – Tomáš Zato Jul 27 '16 at 08:44

3 Answers3

1

If your project has a tsconfig, you probably need another tsconfig for the worker thread. Here is an example for Agular. it is Angular 8, but I tried it in Agular 14 and it still works

https://www.tutorialspoint.com/angular8/angular8_web_workers.htm

William
  • 491
  • 5
  • 9
0

I would bet you can't do that just locally. Though I'm not all sure.. Have you tried in LAMP or similar or even on actual webspace?

https://en.wikipedia.org/wiki/LAMP_(software_bundle)

https://sourceforge.net/projects/wampserver/

  • 2
    i found that it just doesn't work in chrome for locally unless you change the settings. or you can just use safari or firefox – akantoword Jul 26 '16 at 20:58
  • You should be able to do that locally provided you use relative paths. The only exception is google chrome. – Tomáš Zato Jul 27 '16 at 08:45
0

If you're using Angular and you landed on this question:
I also had this issue. I had a tsconfig.worker.json setup and it was linked correctly in angular.json and I followed the Angular Web Worker Documention to initialize the Worker, i.e.
new Worker(new URL('./app.worker', import.meta.url))
The only problem was that Webpack or TSC (not sure which one) didn't resolve the relative names correctly because the baseUrl in my tsconfig.app.json was set to "baseUrl": "" so I changed it "baseUrl": "./" and now it works as it should both in chrome and FF.

AnotherOne
  • 854
  • 2
  • 8
  • 19
  • OP here was clearly not facing an Angular issue (The first Angular version was released a few months after this question was asked). You may consider searching for an existing question facing specifically that issue. If you don't find one, you may even consider asking a new question and self-answer it. – Kaiido Jun 14 '22 at 07:25
  • This was the first result I found when I searched on google for angular webworker and this issue, which means others may land on this exact same post. That's why I posted it here. I editied it, to reflect the fact that's more for those who stumbled on this question. – AnotherOne Jun 14 '22 at 14:53