What is the difference between these headers?
Content-Type: application/javascript
Content-Type: application/x-javascript
Content-Type: text/javascript
Which one is best and why?
Please do not say they are identical – if they were identical there would not have been three of them. I know both work – but I would like to know the difference.
The JavaScript MIME Type
When sending JavaScript content, you should use
text/javascriptas per RFC 9239.Aliases
application/javascript,application/x-javascript,text/javascript1.0,text/javascript1.1,text/javascript1.2,text/javascript1.3,text/javascript1.4,text/javascript1.5,text/jscript, andtext/livescriptare deprecated aliases for it. If you are writing a tool which consumes JavaScript (e.g. an HTTP client) then you should consider supporting them for backwards compatibility.History
The
text/javascriptMIME type was used by convention until RFC 4329 attempted to replace it withapplication/javascript.This was just a change so that the
text/*andapplication/*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).The industry largely ignored the specification so the current specification abandoned the attempt.
X- prefixes
Some of the MIME types mentioned here use an
x-prefix. This was used to indicate experimental MIME types that had not been standardised. As per RFC 6648, this convention is deprecated.HTML
While this question is about HTTP, it is worth mentioning the related
typeattribute in HTML.When loading a traditional script, I recommend you omit the
typeattribute entirely. It has no effect but provides the opportunity to make a typo causing the browser to treat it as pointing to an unrecognised script type and ignore it. If you do provide it, then usetext/javascriptas some deprecated MIME types will not be recognised.When loading a JavaScript module, use
type="module"(note that this value is not a MIME type!).