Engineering / Field note
JavaScript modules need the right Content-Type
A successful HTTP response does not guarantee that a browser can load a JavaScript module. Five fictional response maps separate media-type, syntax and dependency failures.
A browser can receive a JavaScript file successfully and still refuse to load it as a module. The HTTP status answers whether a response arrived successfully. Its Content-Type tells the module loader what kind of resource arrived. Checking only the first can leave a page looking healthy in a request log while its application never starts. [1]
This reference note was reviewed on November 30, 2026. It explains established web behavior, rather than a newly disclosed vulnerability. Our five fictional JSON response maps are original test inputs: each describes an entry URL, response headers and body text, and an expected outcome. They are a compact way to compare cases, not configuration files for a particular server.
A filename is a clue; the response is the evidence
For an ordinary JavaScript module response, the HTML loading algorithm checks the response media type before creating the JavaScript module. The recognized types come from the MIME Sniffing Standard. A .mjs suffix does not turn text/plain into a JavaScript media type. The first case deliberately supplies valid JavaScript under the wrong response label. [1] [2]
{
"fixture": "wrong_media_type",
"entry": "/app.mjs",
"responses": {
"/app.mjs": {
"status": 200,
"contentType": "text/plain",
"body": "document.body.dataset.result = 'ready';"
}
},
"expected": "reject_module_media_type"
}The expected rejection occurs before our small body can set its result marker. When investigating this case, inspect the response for the exact URL the browser requested. A file on disk, a build artifact and the bytes returned through a server are related pieces of evidence, but they answer different questions.
An HTML fallback can wear a successful status
The second case represents a fictional routing mistake: the requested module path receives an HTML page with status 200. Opening the URL may show a perfectly ordinary page. That is useful evidence of the mismatch, not evidence that the JavaScript module works. Read both the response header and the beginning of the body.
{
"fixture": "html_fallback",
"entry": "/app.mjs",
"responses": {
"/app.mjs": {
"status": 200,
"contentType": "text/html",
"body": "<!doctype html><title>Site fallback</title>"
}
},
"expected": "reject_module_media_type"
}Changing that HTML response’s label alone would leave HTML where JavaScript is expected. Our practical recommendation is to establish why the route returned the fallback and restore the intended resource. A media-type error can therefore point to a routing problem as well as a server’s file-type mapping.
A correct type still leaves JavaScript to parse
RFC 9239, published in May 2022, identifies text/javascript as the widely supported media type and treats its other listed types as historical aliases. The HTML script element separately selects module processing through type="module". These labels have different jobs: selecting a JavaScript response type does not certify that the source is valid module syntax. [3] [4]
{
"fixture": "syntax_failure",
"entry": "/app.mjs",
"responses": {
"/app.mjs": {
"status": 200,
"contentType": "text/javascript",
"body": "export const = 3;"
}
},
"expected": "reject_module_syntax"
}Here, export const = 3; is intentionally incomplete JavaScript. Correcting the header gets the response past one boundary; it does not repair the declaration. Keep the browser’s actual error with the response evidence so that a syntax failure is not reported as another media-type failure.
Follow the imports beyond the entry file
The HTML Standard’s external-module-graph procedure follows the entry module’s descendants and links the graph. The same response-type requirement matters when a JavaScript dependency is fetched. An entry file with the right label can therefore lead directly to a child response that stops the graph. [1]
{
"fixture": "child_media_type",
"entry": "/app.mjs",
"responses": {
"/app.mjs": {
"status": 200,
"contentType": "text/javascript",
"body": "import { label } from './label.mjs';\ndocument.body.dataset.result = label;"
},
"/label.mjs": {
"status": 200,
"contentType": "text/plain",
"body": "export const label = 'ready';"
}
},
"expected": "reject_module_media_type"
}Our two-file example makes that distinction visible. The entry imports label from a sibling module and would write it to the document. The sibling’s body is valid JavaScript, but its text/plain response prevents this case from reaching that result. Checking only app.mjs would miss the failing boundary.
Finish with an observable application result
The final case keeps the same import relationship and supplies JavaScript response types for both files. It also includes a small, explicit result: the document’s data-result value should become ready. A successful fetch is evidence of delivery; this marker checks that our particular example actually executed.
{
"fixture": "complete_graph",
"entry": "/app.mjs",
"responses": {
"/app.mjs": {
"status": 200,
"contentType": "text/javascript; charset=utf-8",
"body": "import { label } from './label.mjs';\ndocument.body.dataset.result = label;"
},
"/label.mjs": {
"status": 200,
"contentType": "text/javascript",
"body": "export const label = 'ready';"
}
},
"expected": "execute_with_result_ready"
}We exercised these five response maps in a local Chromium browser using dynamic import() from a page on the same origin. The first, second and fourth failed media-type loading; the third failed parsing; the fifth produced the stated marker. This is bounded fixture verification. It does not establish the behavior of an unexamined deployment or cover every browser policy.
For a real investigation, keep the entry URL, each relevant dependency, HTTP status, returned Content-Type, body and observed application result together. Cross-origin module requests also have CORS requirements, which our same-origin examples do not exercise. That sequence makes the next repair specific: the route, response label, JavaScript source or another browser boundary can be investigated on its own evidence. [4]
Sources + further reading
Source dates describe the original material. Publication above is the date this Zyel article went live.
- HTML Standard: fetching module scriptsWHATWG · Checked Nov 30, 2026
- MIME Sniffing Standard: JavaScript MIME typesWHATWG · Checked Nov 30, 2026
- RFC 9239: Updates to ECMAScript Media TypesIETF / RFC Editor · Checked Nov 30, 2026
- HTML Standard: the script elementWHATWG · Checked Nov 30, 2026
Reader discussion
What would you ask next?
Questions, useful corrections and thoughtful disagreement are welcome. Comments are reviewed before they appear.
No reader comments yet.