The appropriate type to serve JavaScript is "text / javascript" or "application / x-javascript" or "application / javascript"?

11

When analyzing the source code of several pages, Javascript headers vary with respect to the mime (English) , but the question remains about which one we should actually use:

How do we use HTML to indicate a Javascript block < sup> (English)

text/javascript

With application denotation via:

application/x-javascript

or

application/javascript

Question

What is the correct mime to send when we are serving Javascript? Post and Cons of your use?

    
asked by anonymous 03.12.2014 / 14:45

1 answer

7

The "correct" type is application/javascript according to RFC 4329 (% with% is deprecated ). But modern browsers can understand all of them. The reason why "application" is correct is that the browser will run JavaScript code, and it does not need to be read (as text) - for example, a minified JS is not readable (in most cases), but can be run smoothly.

In other words, if you want to be exact, and use the recommended MIME type, then you should use text/javascript . I've put the term "correct" above in quotation marks on purpose - all (to my knowledge) modern browsers will treat the response to a request initiated by a application/javascript as JavaScript, regardless of the Content-Type value in the servers response .

However, it may be that in the future some stricter browser is created, or that a security hole is detected that causes browsers to force the use of the "correct" MIME type, then start using the correct type now can save headaches later.

    
03.12.2014 / 15:53