用 Nginx 部署了一个前端项目,发现 pdf.js 相关的功能异常。
从浏览器的 console 来看,报错信息如下:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of “application/octet-stream”.
Strict MIME type checking is enforced for module scripts per HTML spec.
参考:
https://github.com/mozilla/pdf.js/issues/17296
在前端配置中增加 mjs 相关的配置即可。
server {
# ...
location / {
root /usr/share/nginx/html;
index index.html;
location ~* \.mjs$ {
# target only *.mjs files
# now we can safely override types since we are only
# targeting a single file extension.
types {
text/javascript mjs;
}
}
}
}
mjs 和 js 的区别
mjs 即,MJS(ECMAScript Modules in JavaScript)使用 .mjs 作为文件扩展名,以明确表示该文件使用 ECMAScript 模块系统。