最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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 = `.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?

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./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?

Share Improve this question asked Mar 26, 2019 at 9:27 Jørgen RasmussenJørgen Rasmussen 1,36321 silver badges37 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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./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./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.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论