CSS file blocked: MIME type mismatch (X-Content-Type-Options: nosniff)

123,366

Solution 1

I just ran into the same issue. It appears to be a quirk of Express that can manifest itself for a few different reasons, judging by the number of hits from searching the web for "nodejs express css mime type".

Despite the type="text/css" attribute we put in our <link elements, Express is returning the CSS file as

Content-Type: "text/html; charset=utf-8"

whereas it really should be returning it as

Content-Type: "text/css"

For me, the quick and dirty workaround was to simply remove the rel= attribute, i.e., change

<link rel="stylesheet" type="text/css" href="styles.css">

to

<link type="text/css" href="styles.css">

Testing confirmed that the CSS file was downloaded and the styles did actually work, and that was good enough for my purposes.

Solution 2

I also removed rel = "stylesheet", and I no longer get the MIME type error, but the styles are not being loaded

Solution 3

Some answers suggested removing rel="stylesheet", that didn't work out for me however. According to the expressjs documentation: https://expressjs.com/en/starter/static-files.html use express.static function to serve static files such as CSS, JavaScript,etc...

app.use(express.static('public'))

and from there you should be able to load any file under the public directory for example, if you have a style.css file inside the directory {PROJECT_PATH}/public/css/

http://localhost:3000/css/style.css will work.

Solution 4

In running into the same kind of issue for a full stack web application (in development), I simply solved the problem by correctly linking the css file to the page rendered. Removing the rel = stylesheet, as suggested above, prevents the error to show up in the browser but it does not load the styles that should be applied to the page. In short, it isn't a solution.

If you are using express-static you can use this as an example:

Server-side:

app.use(express.static(__dirname + "/public", {
    index: false, 
    immutable: true, 
    cacheControl: true,
    maxAge: "30d"
}));

Client-side:

 <link type="text/css" rel="stylesheet" href="/main.css">

Just add a forward slash in front of the file you wish to link to the html page (if you are rendering html pages without using any template engines) and express-static will do the rest automatically for you.

Solution 5

Had a similar problem with a javascript file (as opposed to css) in an Angular app. In reality, the problem wasn't with the Mime type (as the outer error message indicated) but was ultimately a "404 Not Found" error.

In my case, putting the script file anywhere but in the "assets" folder resulted in the 404 and eventually the mime type error. The following tag worked for me in the head section of index.html:

<script src="assets/plugins/jquery.min.js" type="text/javascript"></script>

The assets folder is a sibling of the Index.html file.

Share:
123,366
rodrunner
Author by

rodrunner

I like vi

Updated on July 05, 2022

Comments

  • rodrunner
    rodrunner almost 2 years

    I am developing an Angular 4 app and I want to apply some global styles. Following the tutorial at the angular site, I've created a "styles.css" file in the root directory of my app, and I'm referring to that stylesheet in the index.html of my app:

    <link rel="stylesheet" type="text/css" href="styles.css">
    

    The angular app is successfully compiled:

    $ ng serve 
    ** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **
    [...]
    webpack: Compiled successfully.
    

    But when I visit http://localhost:4200 in a Chromium browser, the console shows an error at

    GET http://localhost:4200/styles.css 
    

    In a Firefox browser, the error is a bit more explicit:

    GET 
    http://localhost:4200/styles.css [HTTP/1.1 404 Not Found 15ms]
    The resource from "http://localhost:4200/styles.css" was blocked due to MIME type mismatch (X-Content-Type-Options: nosniff).
    

    Both files, index.html and styles.css are located in the root directory of my angular app. I've tried to get more info about the problem :

    nosniff
        Blocks a request if the requested type is
    
            "style" and the MIME type is not "text/css", or
            "script" and the MIME type is not a JavaScript MIME type.
    

    But I don't understand why it's bloking the request, since I've specified type="text/css" when referencing the stylesheet.