Search code examples
javascriptgoogle-chromemime-typesweb-worker

Chrome 71 will refuse to execute script because its MIME type ('text/x-js') is not executable


I am facing an issue on Chrome 71: in FireFox it works fine, also on Chrome < v71. There was an announcement at Google that we should expect two small changes in our way of working with service workers and cache storage api . But the new version breaks my importScripts and I do not know how to solve it yet.

I got a file named pdfmake-worker-blob.js which makes an:

importScripts('./pdfmake.min.js');
importScripts('./vfs_fonts.js');

onmessage = function(req) {

  pdfMake.fonts = {
      Courier: { normal: 'cour.ttf',bold: 'cour-bold.ttf' },
      Arial: { normal: 'arial.ttf',bold: 'arial-bold.ttf' }
  };

  try {
    var pdf = pdfMake.createPdf(req.data);
    pdf.getBlob(function(output) {
        postMessage(output);
    });
  } catch(err) {
    throw err;
  }
}

Then we create the worker in another service like this before using it:

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

And you can see this in chrome debugger mode(let me remind you once again that the code works fine on firefox and chrome 70 and below I guess): enter image description here

With the following messge in the console and my worker doesn't do his job anymore:

pdfmake-worker-blob.js:1 Deprecated: Future versions will refuse to execute script from 'http://myserver_name.company.com/application_name/scripts/pdfmake.min.js' because its MIME type ('text/x-js') is not executable.

I did not set this Mime type or any i just did an importscripts that is all. I hope it is clearer now.


Solution

  • For those who is facing the problem it is a well konown issue: "strict MIME type checking". Chrome enables strick mime type checking. There is ton of questions on stack over flow about it. But it happens that it was still possible to avoid "strict MIME type checking" through worker api with importScripts.

    For those who are still using chrome 70.0.3538.110 like me there is no strick mime type checking through the importScripts on the worker api. The new version of chrome fixes it. But it is still possible in firefox. I reported this behaviour to mozilla in case it was not expected

    It was not clear for me but in our production machine our apache server has this mime type set: "text/x-js js". I simply replace it with application "application/javascript" js instead or "text/javascript js" which is obsolete by the way.