cssnode.jsangularexpressmime-types

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


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.


Solution

  • 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.