Developer Toolbox

text/javascript

RFC 9239 (2022) made text/javascript the one type for JavaScript and marked application/javascript and the other names obsolete.1 Browsers still accept the old names, but module scripts refuse anything that is not a JavaScript type.2

  • Kind text
  • Extensions .js, .mjs
Open in cURL Builder A request with this type is filled in.

Header

Content-Type: text/javascript; charset=utf-8

At a glance

Parameters
charset; send utf-8. Module scripts are always read as UTF-8.
In a browser
Opened directly, shows the source. Loaded with <script>, runs it.

With curl

curl -I https://example.com/app.js

With fetch

const res = await fetch('https://example.com/app.js');
console.log(res.headers.get('Content-Type'));

Common mistakes

  • Modules need a JavaScript type

    A <script type="module"> or an import is refused when the response has another type, and the console says the server answered with the wrong MIME type. Fix the server's mapping for .js and .mjs.

  • nosniff blocks mislabeled scripts

    With X-Content-Type-Options: nosniff, a classic script served as text/plain or text/html is blocked too. That is the protection you want, so fix the type rather than drop the header.

  • application/javascript still works

    Servers that send it are not broken: browsers accept it. Switch to text/javascript the next time you touch the config.

Sources


  1. 1 RFC 9239, Updates to ECMAScript Media Types, section 2. rfc-editor.org ↩︎
  2. 2 HTML Standard, Fetch a single module script. html.spec.whatwg.org ↩︎