JavaScript ES6 模块 MIME 类型

JavaScript ES6 Modules MIME Type

JavaScript ES6 模块 (*.mjs) 的正确(或更好)MIME 类型是什么:text/javascript application/javascriptjavascript/esm?

参见IANA MIME Type Registry and RFC4329

  • application/javascript 是 JavaScript 的标准。
  • javascript/esm根本不是标准。

使用application/javascript.

但是请注意,对于 HTML you should specify type="module"<script> 元素的 type 属性,根本不提供 MIME 类型。


另请注意 the HTML specification contradicts the MIME standard, and there is an effort to change it backtext/javascript,因此这可能会在未来发生变化。

text/javascript 是符合 the HTML Standard 的正确 JavaScript MIME 类型,其中指出:

Servers should use text/javascript for JavaScript resources. Servers should not use other JavaScript MIME types for JavaScript resources, and must not use non-JavaScript MIME types.

also

[…] the MIME type used to refer to JavaScript in this specification is text/javascript, since that is the most commonly used type, despite it being an officially obsoleted type according to RFC 4329.

正在 IETF 级别的 RFC 中反映这一现实:https://datatracker.ietf.org/doc/draft-ietf-dispatch-javascript-mjs/