0
  • Server: Python/Flask
  • OS: Ubuntu 18.04
  • Vanilla JS
    <script
      type="module"
      src="{{ url_for('static', filename='js/app.js') }}"
    ></script

I have included this script tag at the bottom of the body in the html. For some reasons, when I run the server locally with flask run, I the following error message in browser console:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Why is the loading the wrong MIME-type. I am not even sure if there are more details that are relevant. I will do my best to fill in the blanks as best as I can.

2 Answers 2

1

I found the solution. In Flask if you import files like so

import SomeClass from './utils'

then it will have the content-type in the header set to 'html/text' and it will error. To fix this import it like this:

import SomeClass from './utils.js'

and it will set the content-type header correctly

Sign up to request clarification or add additional context in comments.

Comments

-1

I think adding crossorigin = "anonymous" will fix the problem.

I read that "Unlike classic scripts, module scripts require the use of the CORS protocol for cross-origin fetching."

<script
    type="module"
    src="{{ url_for('static', filename='js/app.js') }}"
    crossorigin="anonymous"
></script>

2 Comments

Thanks for the advice, but no change for me :/
:( maybe this thread has the answer: stackoverflow.com/questions/54651811/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.