ChatGPT解决这个技术问题 Extra ChatGPT

Stylesheet not loaded because of MIME-type

I'm working on a website that uses gulp to compile and browser sync to keep the browser synchronised with my changes.

The gulp task compiles everything properly, but on the website, I'm unable to see any style, and the console shows this error message:

Refused to apply style from 'http://localhost:3000/assets/styles/custom-style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

Now, I don't really understand why this happens.

The HTML includes the file like this (which I am pretty sure is correct):

<link rel="stylesheet" type="text/css" href="assets/styles/custom-style.css"/>

And the stylesheet is a merge between Bootstrap & font-awesome styles for now (nothing custom yet).

The path is correct as well, as this is the folder structure:

index.html
assets
|-styles
  |-custom-style.css

But I keep getting the error.

What could it be? Is this something (maybe a setting?) for gulp/browsersync maybe?

I was getting this when my linked stylesheet started with an html
It happens when you set an incorrect URL to the file or when your server isn't configured properly. In the result, the browser DOESN'T get the stylesheet, but it gets some HTML with 404 status and with the "Content-Type" header. Since the browser gets something from the server, it doesn't tell you there is no reply, but it tells you the MIME type of the file is incorrect. The fastest way to check it is just to try to open the file directly http://localhost:3000/assets/styles/custom-style.css in a new tab.
For me it was the case RussCoder described. The reason behind this was that I used Angular and forgot to add a css file to the styles packaging in the angular.json. So it was ignored while building the app.
I could be able to fix this issue by spring security configurations. It was blocking access to resource folder.
A bad proxy.conf.json setup has led us to this weird error, since the request forwarding to the backend API was not working correctly, thus the HTML error response.

P
Peter Mortensen

For Node.js applications, check your configuration:

app.use(express.static(__dirname + '/public'));

Notice that /public does not have a forward slash at the end, so you will need to include it in your href option of your HTML:

href="/css/style.css">

If you did include a forward slash (/public/) then you can just do href="css/style.css".


href="/assets/style.css"> in my case solved the problem!
Without the "/" I can load the css in pages with url; localhost:3000/api/users. But when I have a link that adds on a variable (.../users?id=5397538945734) it will give the error. When I add the slash in the css link it works. How strange!?
P
Peter Mortensen

The issue, I think, was with a CSS library starting with comments.

While in development, I do not minify files and I don't remove comments. This meant that the stylesheet started with some comments, causing it to be seen as something different from CSS.

Removing the library and putting it into a vendor file (which is ALWAYS minified without comments) solved the issue.

Again, I'm not 100% sure this is a fix, but it's still a win for me as it works as expected now.


An half fix because if you don't want to put all this css in vendor, what should you do? Minimify node_modules each time you test your app? Doh... Did you find something to just compile a certain module?
In my compiling process, the vendor file is created only on build and then i just watch for changes on the files i'm actually working on (which is usually nothing that goes into vendor). This means that the first build is a bit slower, but then i just compile my files without minification which is not slowing up the process for me
I ran into this same issue and found that I was trying to load a minified version of the file that was present on the server as a non-minified file. So, I was trying to load "custom-style.min.css" when the file on the server was "custom-style.css". Once I changed my link to load the correct resource, everything worked fine.
The problem is not limited to CSS. I also got a 404 on a JS file which was caused by a comment in the first line.
the error didn't show up in my case. this works for me. thanks.
P
Peter Mortensen

In most cases, this could be simply the CSS file path is wrong. So the web server returns status: 404 with some Not Found content payload of html type.

The browser follows this (wrong) path from <link rel="stylesheet" ...> tag with the intention of applying CSS styles. But the returned content type contradicts so that it logs an error.

https://i.stack.imgur.com/PHWIq.png


My issue was that the path was invalid. But, this bad path was caused by the base href set for my angular project being incorrect. If anyone else started seeing this error after updating your base href, it may be the cause.
Thanks for taking the trouble to fully and clearly document how a 404 error on the CSS file can produce this (initially) puzzling error message
another way to troubleshoot, paste the url you're getting this error on into another tab, if you're not seeing CSS, this is likely the issue
P
Peter Mortensen

This error can also come up when you're not referring to your CSS file properly.

For example, if your link tag is

<link rel="stylesheet" href="styles.css">

but your CSS file is named style.css (without the second s) then there is a good chance that you will see this error.


Exactly what happened to me. I tried every answer here (change the mimetype, remove CSS comments, change the node.js config...). All I had to do was to fix a typo in the CSS name. Doh!
To add to this answer, be sure gulp actually found the css file and piped it into your dist. Whenever I get this error, it is because I screwed up my path to the css files somehow and it just doesn't exist in the build directory.
Yep, same for me, a simple and stupid typo. I think what it does it that the server send a 404 response page, so your browser get that 404 page and tells you that it's not proper css... which is true.
FWIW, same issue happened to me when Sharepoint permissions were causing an Access Denied. I had to add Authenticated Users to the SP site. As always, THANKS to the Stack Overflow community for pointing me in the right direction. Cheers! 🍺
In my case, I did not upload the style.css to the server. Duh..
P
Peter Mortensen

I had this error for a Bootstrap template.

<link href="starter-template.css" rel="stylesheet">

Then I removed the rel="stylesheet" from the link, i.e.:

<link href="starter-template.css">

And everything works fine. Try this if you are using Bootstrap templates.


According to the Living Standard (4.2.4), "A link element must have either a rel attribute or an itemprop attribute, but not both." So, removing the rel attribute just removes the functionality of including a stylesheet.
S
Sebastian Voráč

I have changed my href to src. So from this:

<link rel="stylesheet" href="dist/photoswipe.css">

to this:

<link rel="stylesheet" src="dist/photoswipe.css">

It worked. I don't know why, but it did the job.


Although it did work for me too, is this a valid attribute?
@sr9yar You are right that according to the validator.w3.org it is not valid to have src in link, so it is good as a quick hotfix, but as soon as you have a bit more time I would suggest to find another more valid workaround.
This is a horribly, INVALID suggestion that needs to be deleted immediately!
P
Peter Mortensen

Make a folder just below/above the style.css file as per the Angular structure and provide a link like <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">.

https://i.stack.imgur.com/yp5Ly.jpg


Why does this work? The error states that "text/html" is not a supported MIME type.
In my case the error went away, the css styles, however, are not applied.. :/
I did spend aome more time reading about this issue and changing the type of a css file ibto something else can cause serious issues, like css being read as html by the server
In AngularDart, the root for index.html is the 'web' folder not the project main folder. So all the css files must be inside the web folder. Like this "[projectfolder]\web[yourcssfileshere]". So if you try to import a css file located outside the web folder it will not be found.
S
Surekha K

Comments in your file will trip this. Some minifiers will not remove comments.

ALSO

If you use Node.js and set your static files using express such as:

app.use(express.static(__dirname + '/public'));

You need to properly address the files.

In my case both were the issue, so I prefixed my CSS links with "/css/styles.css".

Example:

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

This solution is perfect as the path is the main issue for CSS not getting rendering


It is not working for me, I have to put the "public" again in the href html: js app.use('/client', express.static(__dirname + '/client'));
M
Murat Yıldız

In addition to using:

<base href="/">

Remove the rel="stylesheet" part from your css links:

<link type="text/css" href="assets/styles/custom-style.css"/>

Removing rel fixed it, but the question is why?
It silenced the errors but it doesn't apply stylesheets
Removing attribute rel works for me. I'm working with angular.
P
Peter Mortensen

I simply referenced the CSS file (an Angular theme in my case) in the styles section of my Angular 6 build configuration in angular.json:

https://i.stack.imgur.com/RUsFE.jpg

This does not answer the question, but it might be a suitable workaround, as it was for me.


Thats the correct answer for Angular-CLI 6 projects
S
Safwat Fathi

I know it might be out of context but linking a non existed file might cause this issue as it happened to me before.

<!-- bootstrap grid -->
<link rel="stylesheet" href="./css/bootstrap-grid.css" />

If this file does not exist you will face that issue.


Sure, if file is not on the required directory or named different, then you it cause this issue
P
Peter Mortensen

As mentioned solutions in this post, some of the solutions worked for me, but CSS does not apply on the page.

Simply, I just moved the "css" directory into the "Assest/" directory and everything works fine.

<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="assets/css/site.css" >

This is the answer for anyone who wasn't abble to resolve it with other methods!
J
Jared

The problem is that if you have a relative path, and you navigate to a nested page, that would resolve to the wrong path:

<link rel="stylesheet" href='./index.css'>

so the simple solution was to remove the . since mine is a single-page application.

Like this:

<link rel="stylesheet" href='/index.css'>

so it always resolves to /index.css

There are a lot of answers to this question but none of them seem to really work. If you remove rel="stylesheet" it will stop the errors but won't apply the stylesheets.

The real solution:

Just remove the . it works then


Yes, in my case, I forgot to remove these relative paths from my index.html file and was running webpack in the production mode. These paths are not needed as webpack links the css files dynamically through the webpack.prod.js.
P
Peter Mortensen

Also for others using Angular-CLI and publishing to a sub-folder on the webserver, check this answer:

When you're deploying to a non-root path within a domain, you'll need to manually update the <base href="/"> tag in your dist/index.html.

In this case, you will need to update to <base href="/sub-folder/">

https://github.com/angular/angular-cli/issues/1080


P
Peter Mortensen

I got the same issue and then I checked that I wrote:

<base href="./"> in index.html

Then I changed to

<base href="/">

And then it worked fine.


P
Peter Mortensen

I had this problem with a site I knew worked online when I moved it to localhost and PhpStorm.

This worked fine online:

    <link rel="stylesheet" href="/css/additional.css">

But for localhost I needed to get rid of the slash:

    <link rel="stylesheet" href="css/additional.css">

So I am reinforcing a few answers provided here already - it is likely to be a path or spelling mistake rather than any complicated server setup problem. The error in the console is a red herring; the network tab needs to be checked for the 404 first.

Among the answers provided here are a few solutions that are not correct. The addition of type="text/html" or changing href to src is not the answer.

If you want to have all of the attributes so it validates on the pickiest of validators and your IDE then the media value should be provided and the rel should be stylesheet, e.g.:

    <link rel="stylesheet" href="css/additional.css" type="text/css" media="all">

M
Mostafa Ghadimi

I have had the same problem.

If your project's structure is like the following tree:

index.html
assets
|-styles
  |-custom-style.css
server
  |- server.js

I recommend to add the following piece of code in server.js:

var path = require('path')
var express = require('express')
var app = express()

app.use('/assets', express.static(path.join(__dirname, "../assets")));

Note: Path is a built-in Node.js module, so it doesn't need to install this package via npm.


P
Peter Mortensen

You can open the Google Chrome tools, select the network tab, reload your page and find the file request of the CSS and look for what it have inside the file.

Maybe you did something wrong when you merged the two libraries in your file, including some characters or headers not properly for CSS?


unfortunately i did try it but it is not really helping, the request fails instantly and it only has the request url (which is correct)
S
Stig Perez

Adding to a long list of answers, this issue also happened to me because I did not realize the path was wrong from a browser-sync point of view.

Given this simple folder structure:

package.json
app
  |-index.html
  |-styles
      |-style.css

the href attribute inside <link> in index.html has to be app/styles/style.css and not styles/style.css


Indeed , just a typo in the path gave me this same error.
wow this worked for me, thank you so much. I was literally about to give up
That also solves my issue but I don't understand why. I mean the index.html contains a relative path to the style.css. Why I need to put again the "app" folder?
N
Neithan Max

In case you using Express with no JS try with:

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

As an example, my CSS file is at public/stylesheets/app.css


S
Shapon Pal

How I solved this. For Node.js applications, you need to set your **public** folder configuration.

// Express js
app.use(express.static(__dirname + '/public'));

Otherwise, you need to do like href="public/css/style.css".

<link href="public/assets/css/custom.css">
<script src="public/assets/js/scripts.js"></script>

Note: It will work for http://localhost:3000/public/assets/css/custom.css. But couldn't work after build. You need to set app.use(express.static(__dirname + '/public')); for Express


C
CodesmithX

At times, this happens when the css file is not found. It's worth checking your base url / path to the file


P
Peter Mortensen

For a Node.js application, just use this after importing all the required modules in your server file:

app.use(express.static("."));

express.static built-in middleware function in Express and this in your .html file:


You don't really want to serve you server code to the public do you ?
b
bahri noredine

by going into my browsers console > network > style.css ...clicked on it and it showed "cannot get /path/to/my/CSS", this told me my link was wrong. i changed that to the path of my CSS file.

Original path before change was localhost:3000/Example/public/style.css changing it to localhost:3000/style.css solved it.

if you are serving the file from app.use(express.static(path.join(__dirname, "public"))); or app.use(express.static("public")); your server would pass "that folder" to the browser so adding a "/yourCssName.css" link in your browser solves it

By adding other routes in your browser CSS link, you'd be telling the browser to search for the css in route specified.

in summary... check where your browser CSS link points to.


m
manbradcalf

This is specific to Typescript+Express

I ctrl+f'd "Typescript" and ".ts" and found nothing in these answers, so I'll add my solution here, since it was caused by (my inexperience with) typescript, and the solutions I've read don't explicit solve this particular issue.

The problem was that Typescript was compiling my app.ts file into a javascript file in my project's dist directory, dist/app.js

Here's my directory structure, see if you can spot the problem:

├── app.ts
├── dist
│   ├── app.js
│   ├── app.js.map
│   └── js
│       ├── dbclient.js
│       ├── dbclient.js.map
│       ├── mutators.js
│       └── mutators.js.map
├── public
│   ├── css
│   │   └── styles.css
├── tsconfig.json
├── tslint.json
└── views
    ├── index.hbs
    └── results.hbs

My problem is that in app.ts, I was telling express to set my public directory as /public, which would be a valid path if Node actually were running Typescript. But Node is running the compiled javascript, app.js, which is in the dist directory.

So having app.ts pretend it's dist/app.js solved my problem. Thus, I fixed the problem in app.ts by changing

app.use(e.static(path.join(__dirname, "/public")));

to

app.use(e.static(path.join(__dirname, "../public")));

T
Tadele Ayelegn

https://github.com/froala/angular-froala/issues/170#issuecomment-386117678 Found the above solution of adding

href="/">

Just before the style tag in index.html

https://i.stack.imgur.com/8wrOx.png


This worked for me for a site I was building with Go Hugo.
A
Alex Gongadze

I was working with the React.js app and also had this error which led me here. This is what helped me. Instead of adding <link> to the index.html I added an import to the component where I need to use this stylesheet:

import 'path/to/stylesheet.css';

S
Sanjay Choubey

Remove rel="stylesheet" and add type="text/html". So it will look like this -

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

P
Peter Mortensen

In my case, when I was deploying the package live, I had it out of the public HTML folder. It was for a reason.

But apparently a strict MIME type check has been activated, and I am not too sure if it's on my side or by the company I am hosting with.

But as soon as I moved the styling folder in the same directory as the index.php file I stopped getting the error, and styling was activated perfectly.


P
Peter Mortensen

If you are setting Styles in JavaScript as:

    var cssLink = document.createElement("link");
    cssLink.href = "./content.component.scss";
    cssLink.rel = "stylesheet";
   --> cssLink.type = "html/css";
    (iframe as HTMLIFrameElement).contentDocument.head.appendChild(cssLink);

Then just change cssLint.type (denoted by arrow in above description) to "MIME":

   cssLink.type = "MIME";

It will help you to get rid of the error.