Skip to content

Can't Serve Files Locally in Python Due to CORS #400

@aderantcurt

Description

@aderantcurt

If you're trying to serve these files locally using python, you may encounter this error in the Chrome console because of CORS attempting to serve TEXT file MIME types instead of JS file types.

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

If you want to serve these using python and run it locally in Chrome, this may work for you:

Create a file named custom_server.py with these contents:

# From
# https://stackoverflow.com/a/60638762/2387067
#Use to create local host
import http.server
import socketserver

PORT = 8000
SERVER_ADDRESS = "127.0.0.1"

Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
      ".js": "application/javascript",
});

httpd = socketserver.TCPServer((SERVER_ADDRESS, PORT), Handler)
httpd.serve_forever()

and then run it

python -m custom_server.py

Then browse to:

http://127.0.0.1:8000/

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions