When Serving JavaScript Files, Is It Better to Use the Application/JavaScript or Application/X-Javascript

When serving JavaScript files, is it better to use the application/javascript or application/x-javascript

According to the IETF's ECMAScript Media Types Updates as of 22 February 2021, the RFC-4329 is obsolete.

Therefore:

  • text/javascript is a recommended standard (both by IETF and by MDN)
  • application/x-javascript was experimental while deciding to move to…
  • application/javascript is obsolete

application/javascript or text/javascript

The MDN article links to the WHATWG (Web Hypertext Application Technology Working Group, which consists of people from Apple, Google, Mozilla, Microsoft and others) HTML Living Standard that was last updated a few days ago. This is the current most up to date HTML specification and it says text/javascript.

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.

In reality though no browser will break on either text/javascript or application/javascript so it doesn't really matter. For new projects I would definitely use text/javascript but I won't go back and update my old ones.

When to use the JavaScript MIME type application/javascript instead of text/javascript?

In theory, according to RFC 4329, application/javascript.

The reason it is supposed to be application is not anything to do with whether the type is readable or executable. It's because there are custom charset-determination mechanisms laid down by the language/type itself, rather than just the generic charset parameter. A subtype of text should be capable of being transcoded by a proxy to another charset, changing the charset parameter. This is not true of JavaScript because:

a. the RFC says user-agents should be doing BOM-sniffing on the script to determine type (I'm not sure if any browsers actually do this though);

b. browsers use other information—the including page's encoding and in some browsers the script charset attribute—to determine the charset. So any proxy that tried to transcode the resource would break its users. (Of course in reality no-one ever uses transcoding proxies anyway, but that was the intent.)

Therefore the exact bytes of the file must be preserved exactly, which makes it a binary application type and not technically character-based text.

For the same reason, application/xml is officially preferred over text/xml: XML has its own in-band charset signalling mechanisms. And everyone ignores application for XML, too.

text/javascript and text/xml may not be the official Right Thing, but there are what everyone uses today for compatibility reasons, and the reasons why they're not the right thing are practically speaking completely unimportant.

text/javascript vs application/javascript

Per IETF RFC 9239 text/javascript is now standard and application/javascript is now considered obsolete.

The media type registrations herein are divided into two major categories: (1) the sole media type
"text/javascript", which is now in common usage and (2) all of the media types that are obsolete
(i.e., "application/ecmascript", "application/javascript", "application/x-ecmascript", "application/
x-javascript", "text/ecmascript", "text/javascript1.0", "text/javascript1.1", "text/javascript1.2", "text/
javascript1.3", "text/javascript1.4", "text/javascript1.5", "text/jscript", "text/livescript", and "text/xecmascript").

See further notes in When serving JavaScript files, is it better to use the application/javascript or application/x-javascript

what is difference between text/javascript and application/javascript

text/javascript was introduced when the web was young and people hadn't thought things through.

Then people thought things through, and decided that text/* should be reserved for things designed to be human readable (which is why some XML is text/xml and other XML is application/xml). JavaScript is not human readable, so text/javascript was deprecated and application/javascript was introduced to replace it.

Years later, some browsers still haven't caught up.

You can configure your server to always serve application/javascript in the HTTP headers; browsers that don't support it also pay no attention to the actual content-type.

For the time being, if you are writing HTML 4 or XHTML 1, specify text/javascript in the type attribute for the sake of backwards compatibility. If you are writing HTML 5, then omit the type attribute (as it is now optional).

Difference between application/x-javascript and text/javascript content types

text/javascript is obsolete, and application/x-javascript was experimental (hence the x- prefix) for a transitional period until application/javascript could be standardised.

You should use application/javascript. This is documented in the RFC.

As far a browsers are concerned, there is no difference (at least in HTTP headers). This was just a change so that the text/* and application/* MIME type groups had a consistent meaning where possible. (text/* MIME types are intended for human readable content, JavaScript is not designed to directly convey meaning to humans).

Note that using application/javascript in the type attribute of a script element will cause the script to be ignored (as being in an unknown language) in some older browsers. Either continue to use text/javascript there or omit the attribute entirely (which is permitted in HTML 5).

This isn't a problem in HTTP headers as browsers universally (as far as I'm aware) either ignore the HTTP content-type of scripts entirely, or are modern enough to recognise application/javascript.

Difference between application/javascript and text/javascript

  • text/javascript - is obsolete
  • application/javascript - MIME type for JS

Guidelines:

  • Use application/javascript on the server to return MIME type
  • Use text/javascript for type attributes of <script> tag although it's not really required

What is the proper content type for JavaScript in HTML?

Beside that application/javascript is the official MIME type of JavaScript , most browsers only accept text/javascript or just ignore the type attribute value.

What is an appropriate content-type header for JavaScript files?

According to the IANA Registered MIME media type list, JS has two registered MIME types; the obsolete text/javascript and the now official application/javascript.

Thus, use application/javascript.

JavaScript ES6 Modules MIME Type

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/



Related Topics



Leave a reply



Submit