Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript MIME type warning on Firefox

I am loading the java-script file in Django template:

<script type="application/javascript" src="{% static 'online-v3.js' %}"></script>

It is loading properly on Chrome. But, on Firefox I get the following warning:

The script from “http://127.0.0.1:8000/static/myscript.js” was loaded even though its MIME type (“text/plain”) is not a valid JavaScript MIME type.

I fear that due to this problem, on some browser the JS file might not load at all.

What is the possible reason for this and how can I solve this issue?

like image 888
Ranveer Singh Avatar asked Aug 09 '19 14:08

Ranveer Singh


2 Answers

Remove the type or change it to "text/javascript".

In html5 spec the type is not required unless it is not javascript

like image 75
charlietfl Avatar answered Oct 16 '22 12:10

charlietfl


To add onto evilpie's answer, the root cause for me was using the basic py -m http.server for local testing. In order to correctly assign .js files the 'text/javascript' MIME type, I instead use this script from Github user HaiyangXu.

# -*- coding: utf-8 -*-
#test on python 3.4 ,python of lower version  has different module organization.
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver

PORT = 8080

Handler = http.server.SimpleHTTPRequestHandler

Handler.extensions_map={
        '.manifest': 'text/cache-manifest',
    '.html': 'text/html',
        '.png': 'image/png',
    '.jpg': 'image/jpg',
    '.svg': 'image/svg+xml',
    '.css': 'text/css',
    '.js':  'application/x-javascript',
    '': 'application/octet-stream', # Default
    }

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

Source: https://gist.github.com/HaiyangXu/ec88cbdce3cdbac7b8d5

like image 21
Andrew Patton Avatar answered Oct 16 '22 10:10

Andrew Patton