Web workers don't work
See original GitHub issueDescribe the bug Web workers don’t work, I get the following error:
testWorker.js:1
Uncaught SyntaxError: Unexpected token <
When I click on the error, I see the code from the index.html
for some reason:
structure
App.vue
workers
|_ testWorker.js
App.vue
mounted() {
let testWorker = new Worker('workers/testWorker.js')
testWorker.postMessage('message from App.vue')
testWorker.addEventListener("message", (event) => {
console.log("Received data from worker:", event)
})
}
testWorker.js
onmessage = function (event) {
postMessage({ result: 'Reply from worker' })
}
Environment:
- OS and version: win10
- vue-cli-plugin-electron-builder version : 1.4.0
- electron version: 6.0.7
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Web Worker not working [closed] - Stack Overflow
my code of javascript is not working, i expect it to start counting from 1 to infinite when the user clicks the "start...
Read more >Web Workers not working · Issue #5180 · parcel-bundler/parcel
bug report If you create a worker and use #3654 it seems to inject react-refresh and breaks the worker: Worker1: Uncaught ReferenceError: ...
Read more >The State Of Web Workers In 2021 - Smashing Magazine
Workers have a bad rep, but can be an important and useful tool in any web developer's toolbelt for these kinds of problems....
Read more >Using Web Workers - Web APIs - MDN Web Docs
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering ......
Read more >web workers on Chrome don't work locally - Google Groups
web workers on Chrome don't work locally. 1645 views ... webserver and view the page through my localhost, a web worker process
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Never mind. I figured out how to make it work. For anyone else having troubles with this, here’s what I did:
worker-loader
:vue.config.js
:background.js
(main Electron file), insidewebPreferences: {}
of the window, addnodeIntegrationInWorker: true
if you want to be able to use Node’s modules inside the worker. Create a worker file (e.g.workers/worker1.js
) and create a listener in there, here’s some example:Notes: Don’t try to use node’s native
worker_threads
, they don’t work in ElectronTags for people who’s trying to find it:
Thanks for providing such a detailed guide! I will link to this in the docs when I get the chance.