Search code examples
iosipadionic-frameworkweb-worker

Ionic worker's creation on wkwebview on ios 11: SecurityError (DOM Exception 18): The operation is insecure


I am facing SecurityError (DOM Exception 18): The operation is insecure. There are a lot of issues related "to dom security exception" but none fit my need.

First, I had a drop down list problem in IOS 11.0.3 thanks to this link. I found a way to resolved it.

But now when I am trying to create a worker i have a SecurityError (DOM Exception 18). The exception occured when i did :

var pdfWorkerBlob = new Worker('scripts/pdfmake-worker-blob.js');

By the way it is a worker we use for printing pdf with the pdfmakejs. This line worked fine before I use the cordova-plugin-wkwebview-engine I am using ionic. The result of the following command is:

ionic info 
cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-plugin-proxy : 1.5.8
    @ionic/cli-utils        : 1.19.2
    ionic (Ionic CLI)       : 3.20.0

System:

    Node : v8.7.0
    npm  : 5.7.1
    OS   : macOS High Sierra

Here is a screenshot of the exception

enter image description here


Solution

  • I did find a way to resolve my problem.

    In the case you are facing this problem not in ionic. The best approach seems that you have to deploy a light local webserver to serve your scripts.

    Caddy can be a good tool for that. Thanks to the @Matt(Matt Holt) by the way. This solution was suggested by him in a topic on github and it inpires me for my solution.

    Having said that, as we are using ionic. We already have a local web server that can serve the script.

    First I get the application url with:

    var rootUrl=document.URL.split('#')[0];
    

    And then i can create my worker like this :

    pdfmakeWorkerBlob = new Worker(rootUrl+'scripts/pdfmake-worker-blob.js');
    

    And no more dom exception security problem.

    For those who are not familiar witth web workers this page is a great start