Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a "df.worker.min.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type"

I try to use pdfjs in a small typescript app with parceljs as bundler, but when I load the worker with:

pdfjsLib.GlobalWorkerOptions.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.min.js';

I get this error in the Firefox console:

pdf.worker.min.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type

and the worker is not loaded.

If I load the worker like this:

pdfjsLib.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${(pdfjsLib as any).version}/pdf.worker.min.js`;

everything works fine.

I have looked at the MDN description of the error and I sounds like some server side thing, so is it a limitation of the parcel server and is there a workaround?

like image 449
Jørgen Rasmussen Avatar asked Mar 26 '19 09:03

Jørgen Rasmussen


2 Answers

Check the file permissions of that .js file on your websever.

I had a similar issue where loading one of my .js files failed and Firefox printed the message:

The script from “https://my.website.com/file.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.

Loading failed for the <script> with source “https://my.website.com/file.js”.

All other .js files were sent with the correct content-type "application/javascript", but this one file was sent with "text/html".

The problem was, that while transferring the file to my webserver (using FileZilla) the permissions of the file were set to 600. Setting the file permissions to 705 fixed it. The .js file is now sent with the correct content-type and the Firefox message is gone.

like image 96
Rckstr Avatar answered Nov 19 '22 17:11

Rckstr


It's happening because of the wrong content-type response header.

Your server is responding with a javascript file with content-type: "text/html" which is wrong.

Change the content-type to "text/javascript".

It should work fine.

Update (11-APR-2019):

I am assuming you are using express framework for http-server. content-type can be set using a middleware.

app.use('*.js', (req, res, next) => {
    res.set('Content-Type', 'text/javascript')
    next();
})

Add this before the static file serving middleware.

like image 32
Srujan Reddy Avatar answered Nov 19 '22 18:11

Srujan Reddy