Issue
I have this html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
rel="stylesheet"
href="../../../static/css/style.css"
type="text/css" />
<link
rel="stylesheet"
href="../../../static/css/user_header.css"
type="text/css" />
<!--suppress HtmlUnknownTarget -->
<link rel="shortcut icon" href="/favicon.png">
<script type="module" src="../../../static/js/node_connect.js" ></script> <-- Error
<script src="../../../static/lib/jquery.min.js"></script>
<script src="../../../static/lib/selfserve.js"></script>
</head>
The problematic bit is the node_connect.js
file. When starting the Flask web tool locally (Python 3.7.2), the following error is given in the console when opening the page:
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.
Checking the headers:
Content-Type: text/plain; charset=utf-8
However, on production (when started through Gunicorn) it gives this:
Content-Type: application/javascript; charset=utf-8
I am guessing the web-server (Apache) serves them in the production case, but another thing is when testing other pages of this web tool, they all work all and load the javascript files correctly (even if their content-type is text/plain). However the difference is, I noticed, in the type.
This works in my case:
<script src="../../static/js/translator/library/materialize.js"></script>
Or this:
<script type="application/javascript" src="../../static/js/translator/library/materialize.js"></script>
Of course, I tried this for the problematic javascript
file and received the following error (which means it now loaded):
Uncaught SyntaxError: Cannot use import statement outside a module
This, as far as I researched, basically means I need to set the type to module
(however this makes the browser decline the .js file).
Could anyone please help me with this?
Solution
Here's another trick without needing to change anything in registry:
put this before importing flask:
# fix windows registry stuff
import mimetypes
mimetypes.add_type('application/javascript', '.js')
mimetypes.add_type('text/css', '.css')
Answered By - Harel Ashwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.