In our Ionic app, we have a web worker to do some calculations.
This used to work fine, but now suddenly there are some problems. There were quite a few changes throughout our app, but I have deleted most code to pinpoint the error, but I didn't find out much.
What happens is that everything seems like it's working ok, but the code inside the web worker is never executed. There is also no error thrown. When I log the web worker when it does not work, Chrome logs > Worker {}
, if it does work, it logs > Worker
. So it looks like the web worker is not initialized for some reason.
Code inside app:
const libWorker = new Worker('./assets/workers/lib.js')
console.log(libWorker) // Logs either "Worker {}" or "Worker"
libWorker.onmessage = event => {
console.log('WORKER CALLBACK')
}
libWorker.postMessage({ ... })
Code in webworker:
console.log("worker started!")
The problem is:
Almost every time this code runs, worker started!
is not logged and it seems as if the web worker is not initialized, but there is no error.
Here's what I know:
console.log("worker started!");
setTimeout
s to make sure that everything is ready and has enough time to load.What really makes this difficult to debug is that there is never any error message if the web worker is not initialized. Only when it IS initialized (around 5% of the times) and the web worker code contains a syntax error, then it throws an error. So it looks like the new Worker()
is not actually called most of the time for some reason.
This issue was magically resolved over the weekend.
I think the issue was that the web worker code was somehow cached and therefore did not reflect the changes I made to the app.