Reputation: 211
I'm instantiating a module like so in index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello Module</title>
</head>
<body>
<script type="module" src="./index.js"></script>
</body>
</html>
index.js
is empty.
When I serve this via py -3 -m http.server
(Python 3.8.5), I get the Chrome error:
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
I've read what this error signifies. I do not know how to configure http.server because heretorfore it's been a black box to me. Am I past the point at which Python's default (?) server is helpful?
Upvotes: 4
Views: 3442
Reputation: 169338
JavaScript should be served with the content-type text/javascript
.
The default http.server.SimpleHTTPRequestHandler
handler may not (since mappings can be read from the Windows registry) have a mapping for the .js
extension to a file type, or it might be wrong (as evident from text/plain
).
You'll need to write your own short script to patch in the .js
extension, and use that instead of python -m http.server
, something like:
import http.server
HandlerClass = http.server.SimpleHTTPRequestHandler
# Patch in the correct extensions
HandlerClass.extensions_map['.js'] = 'text/javascript'
HandlerClass.extensions_map['.mjs'] = 'text/javascript'
# Run the server (like `python -m http.server` does)
http.server.test(HandlerClass, port=8000)
Upvotes: 11