简体   繁体   中英

WebWorker is only loaded sometimes on android

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:

  • The code does work sometimes on Android (about 5% of the times or less), so the imports and usage are correct.
  • I have deleted all the code inside the webworker except for a console.log("worker started!");
  • It is most likely not a timing issue, I tried to wrap the code into multiple setTimeout s to make sure that everything is ready and has enough time to load.
  • The same code works flawlessly, 100% of the time, on iOS and in the Browser (Chrome).
  • There is no error logged in either case (I attached an event listener to listen to errors)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM