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?
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?
Share Improve this question edited Jul 30, 2020 at 6:14 The Most Curious Thing asked Jul 30, 2020 at 5:32 The Most Curious ThingThe Most Curious Thing 2113 silver badges14 bronze badges 6- Please show us the server code too. – AKX Commented Jul 30, 2020 at 5:36
- See issue35403 on the Python issue tracker - you may need to use Python 3.8 as per the resolution. – metatoaster Commented Jul 30, 2020 at 5:42
- @AKX That's my point of confusion; I don't know where or what http.server's code is. – The Most Curious Thing Commented Jul 30, 2020 at 5:45
-
No, please show the code where you use
http.server
. – AKX Commented Jul 30, 2020 at 5:46 - @AKX Okay, my question is more thorough now. – The Most Curious Thing Commented Jul 30, 2020 at 6:15
1 Answer
Reset to default 11JavaScript 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)