10

What is the right (or better) MIME Type for JavaScript ES6 modules (*.mjs): text/javascript, application/javascript or javascript/esm?

Rosberg Linhares
  • 3,537
  • 1
  • 32
  • 35
  • 6
    Accoding to [mdn](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types) the mime type of `.mjs` and `.js` is `text/javascript` – evolutionxbox Jan 16 '20 at 16:36
  • @evolutionxbox — That's fixed now. – Quentin Jan 16 '20 at 16:43
  • @evolutionxbox, _text/javascript_ is [obsolete](https://tools.ietf.org/html/rfc4329#section-7.1). – Rosberg Linhares Jan 16 '20 at 16:44
  • @Quentin nicely done! – evolutionxbox Jan 16 '20 at 16:45
  • 1
    the way a browser decides whether something is a module is not by looking onto a MIME Type but by checking `type` attribute of the script tag. You should use standard MIME Type for javascript. – marzelin Jan 16 '20 at 16:51
  • @RosbergLinhares it might be obsolete in RFCs but browser vendors still recommend it. https://v8.dev/features/modules#mjs – marzelin Jan 16 '20 at 16:59
  • 2
    Actually, `application/javascript` (RFC 4329) is obsolete, and `text/javascript` is the correct one. See my answers for details. I’ve reverted the change on MDN — you’re not the first to make this mistake :) – Mathias Bynens Jan 16 '20 at 17:10
  • @marzelin Browsers do check the MIME type in the `Content-Type` header for JS modules. – Mathias Bynens Jan 16 '20 at 17:11
  • @MathiasBynens yes, but `Content-Type` has nothing to do with deciding whether some file is a module or not. – marzelin Jan 16 '20 at 17:14
  • 1
    @marzelin That’s what I’m saying — for web browsers, it has everything to do with it, as defined in the HTML Standard. Without a proper JavaScript MIME type, the module won’t execute. Try this: `` – Mathias Bynens Jan 16 '20 at 17:18
  • @MathiasBynens what I'm saying is that `Content-Type` decides whether a file can be executed (the server allows it to be executed by providing proper MIME) but it has nothing to do whether a browser executes it as a module or standard js script. In other words, nothing you put in `Content-Type` will make the browser run it as a module if you don't specify proper `type` on `script` tag. – marzelin Jan 16 '20 at 17:23
  • 2
    @HereticMonkey, no, because this question is specific for JavaScript modules (*.mjs), which could have a different semantic. Moreover, aparently that other question does not provide the right answer for the subject. – Rosberg Linhares Jan 16 '20 at 17:39
  • @RosbergLinhares It does now that Mathias has answered it. "which could have a different semantic" it doesn't, so the answer to that question answers this one. – Heretic Monkey Jan 16 '20 at 17:40
  • @HereticMonkey, , 1. The [MDN website](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) says: *".mjs files need to be loaded with a MIME-type of javascript/esm (or another JavaScript-compatible MIME-type such as application/javascript)"*. 2. Webpack states to use *javascript/esm* to represent EcmaScript modules: See [Rule.type](https://webpack.js.org/configuration/module/#ruletype) and [New Module Types](https://scotch.io/tutorials/whats-new-in-webpack-4#toc-new-module-types). – Rosberg Linhares Jan 16 '20 at 18:27
  • @HereticMonkey 3. Modules have even a different extension, which could lead people to think if there would be a different MIME type for it. IMHO, this type of confusion is related only to JavaScript modules, and this question would be useful to give the answer for this specific case. – Rosberg Linhares Jan 16 '20 at 18:28
  • There are all things you should [edit] into your question to show that it is different from the duplicate. See [the help center article on duplicates](https://stackoverflow.com/help/duplicates) and [the FAQ](https://meta.stackoverflow.com/q/252252/215552) – Heretic Monkey Jan 16 '20 at 18:41
  • 2
    The `javascript/esm` part is wrong; that is not a valid JavaScript MIME type. I've fixed that on MDN. – Mathias Bynens Jan 16 '20 at 18:58
  • @MathiasBynens you should also fix type for *.mjs* https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types – marzelin Jan 16 '20 at 19:27
  • @marzelin Thanks for the pointer; done. Note that MDN is a wiki, so you could make edits yourself ;) `` – Mathias Bynens Jan 16 '20 at 22:26
  • Ok @MathiasBynens, thank you so much! – Rosberg Linhares Jan 16 '20 at 22:29

2 Answers2

11

text/javascript is the correct JavaScript MIME type per the HTML Standard, which states:

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.

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

Work is underway to reflect this reality in an RFC at the IETF level: https://datatracker.ietf.org/doc/draft-ietf-dispatch-javascript-mjs/

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • Are modules an exception when the browser is set to not check the mime type (which option should not be used, ofcourse)? – Teemu Jan 16 '20 at 17:17
  • @Teemu Which setting are you referring to exactly? – Mathias Bynens Jan 16 '20 at 17:20
  • This answer is needed on the proposed duplicate [When serving JavaScript files, is it better to use the application/javascript or application/x-javascript](https://stackoverflow.com/questions/876561/when-serving-javascript-files-is-it-better-to-use-the-application-javascript-or) – Heretic Monkey Jan 16 '20 at 17:24
  • Hmm ... it looks like the option is not available anymore. – Teemu Jan 16 '20 at 17:26
  • 1
    @HereticMonkey Posted it here: https://stackoverflow.com/a/59774937/96656 – Mathias Bynens Jan 16 '20 at 17:28
-2

See the IANA MIME Type Registry and RFC4329.

  • application/javascript is a standard for JavaScript.
  • javascript/esm is not a standard at all.

Use application/javascript.

Note, however, that for a type attribute for a <script> element in HTML you should specify type="module" and not provide a MIME type at all.


Note also that the HTML specification contradicts the MIME standard, and there is an effort to change it back to text/javascript so this may change in future.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335