我试图在火狐中使用ES6模块,但它不起作用(WTF:除非从磁盘加载)。我把它归结为MWE,但我看不出它有什么问题:
HTML index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>title</title>
<script type="module" src="main.js"></script>
</head>
<body>
<p id="status">failed</p>
</body>
</html>模块file.js
export const x = 42;主脚本main.js
import { x } from './file.js';
window.onload = function() {
document.getElementById('status').textContent = x;
};现在,所有这些文件都由NGINX正确地处理:
$ cmp main.js <(curl 'localhost:8080/main.js' 2>/dev/null)
$ cmp file.js <(curl 'localhost:8080/file.js' 2>/dev/null)
$ cmp index.html <(curl 'localhost:8080/index.html' 2>/dev/null)但它在Firefox中不起作用。在火狐中转到http://localhost:8080/不会启动脚本,并且它的控制台上只显示以下消息:
Loading failed for the module with source “http://localhost:8080/main.js”. localhost:8080:7没有更多的信息。
Network显示加载了main.js,但file.js甚至没有试过(也就是说,它不会失败加载)。
当然,dom.moduleScripts.enabled;true in about:config。
令人惊讶的是,如果让FF直接加载这些东西,它就能工作:
$ firefox index.html显示了我期待看到的42。
使用的版本:
- NGINX$ nginx -v nginx版本: nginx/1.14.0
发布于 2018-09-13 11:22:12
查看来自Content-Type服务器的header。改变它解决了问题。一定是text/javascript。感谢克里斯G的有益评论。
发布于 2019-01-20 10:55:58
同样的问题,不同的场景:我在Firefox控制台中使用webpack和我得到了相同的警告,因为我链接了我的脚本,而不是捆绑文件。
新手犯了错误,但错了。也许能帮到别人。
https://stackoverflow.com/questions/52312102
复制相似问题