Search code examples
javascriptangularjsinternet-explorer-11web-worker

ng-webworker - IE 11 errors


So I'm using a library called ng-webworker and attempting to run a very simple long running task.

$scope.onParallelDownload = function() {
   function doubler(num) {
       return num * 2;
                         }

   var myWorker = webWorker.create(doubler);
   myWorker.run(3).then(function(result) {
         alert("Answer: " + result);

   }, function(error) {
       var err = error;
   });
}

This works perfectly in Chrome and shows the alert, but when run in Internet Explorer 11, where I am debugging it the error function is hit, which was still promising, however, there is no data given in the error payload which is problematic because I've absolutely no idea what is causing the web worker to fail on that particular browser.


Solution

  • Most likely you did not set the path to the file worker_wrapper.min.js (or worker_wrapper.js). This file is required for IE (see below). Adjust your app config to the following: angular.module('myApp', [ // your dependencies 'ngWebworker' ]) .config(['WebworkerProvider', function (WebworkerProvider) { WebworkerProvider.setHelperPath("./bower_components/ng-webworker/src/worker_wrapper.min.js"); // adjust path }]); This code assumes you installed ngWebworker with bower. You might still have to adjust the path, depending on the path you are in.

    If you've already set the helper path but it still does not work, check if helper file is being loaded in the developer tools (you might have set a wrong path and get a 404).

    Details

    When passing a function to Webworker, it transforms this function into a blob which is then executed by the web worker as if it were an independent file. However, Internet Explorer treats these blobs as cross-domain, so this does not work. The workaround that ngWebworker uses is to run an independent JavaScript file (the worker_wrapper.min.js we set above). The web worker then runs that file and ngWebworker passes your stringified function to the worker where it is evaluated.

    Note that if you're not on IE, this file will not be used.