问题
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?
回答1:
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.
回答2:
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".
回答3:
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.
回答4:
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">.
回答5:
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.
回答6:
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.
回答7:
I have changed my 'href' -> '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.
回答8:
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:
This does not answer the question, but it might be a suitable workaround, as it was for me.
回答9:
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
回答10:
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" >
回答11:
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
回答12:
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">
回答13:
My problem is that I was using webpack and in my HTML CSS link I had a relative path, and anytime I would 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
回答14:
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
回答15:
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?
回答16:
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.
回答17:
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.
回答18:
Remove rel="stylesheet" and add type="text/html". So it will look like this -
<link href="styles.css" type="text/html" />
回答19:
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: <
link rel="stylesheet" href="style.css">
回答20:
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.
回答21:
One of the main reasons for the issue is the CSS file which is trying to load isn't a valid CSS file.
Causes:
- Invalid MIME type
- Having JavaScript code inside style sheet - (may occur due to incorrect Webpack bundler configuration)
Check the file which you're trying to load is a valid CSS style sheet (get the server URL of the file from the network tab and hit in a new tab and verify).
Useful info for consideration when using <link> inside the body tag.
Though having a link tag inside the body is not the standard way to use the tag. But we can use it for page optimization (more information: https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery) / if the business use case demands (when you serve the body of the content and server configured to have to render the HTML page with content provided).
While keeping inside the body tag we have to add the attribute itemProperty in the link tag like
<body>
<!-- … -->
<link itemprop="url" href="http://en.wikipedia.org/wiki/The_Catcher_in_the_Rye" />
<!-- … -->
</body>`
For more information on itemProperty have a look in https://webmasters.stackexchange.com/questions/55130/can-i-use-link-tags-in-the-body-of-an-html-document.
回答22:
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
回答23:
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.
回答24:
Bootstrap styles not loading #3411
https://github.com/angular/angular-cli/issues/3411
I installed Bootstrap v. 3.3.7
npm install bootstrap --saveThen I added the needed script files to
apps[0].scriptsin the angular-cli.json file:"scripts": [ "../node_modules/bootstrap/dist/js/bootstrap.js" ], // And the Bootstrap CSS to the apps[0].styles array "styles": [ "styles.css", "../node_modules/bootstrap/dist/css/bootstrap.css" ],I restarted ng serve
It worked for me.
回答25:
This issue happens when you're using cli tool for either reactjs or angular, so the key is to copy the entire final build from those tools since they initialize they're own lite servers which confuses your URLs with back end server you've created... take that whole build folder and dump it on asset folder of your back end server project and ref them from your back end server and not the server which ships with Angular or Reactjs Otherwise you're using it as front end from a certain API server
回答26:
Triple check the name and path of the file. In my case I had something like this content in the target folder:
lib
foobar.bundle.js
foobr.css
And this link:
<link rel="stylesheet" href="lib/foobar.css">
I guess that the browser was trying to load the JavaScript file and complaining about its MIME type instead of giving me a file not found error.
回答27:
I had the same problem and after hours of debugging I found out that it is related to temporary files not able to be written.
Go to Admin → Config → File system. Set the correct temporary directory. Make sure your server has permission to write to that directory.
回答28:
I had this error, in Angular. The way I solved it was to put an ngIf on my link element so it didn't appear in the DOM until my dynamic URL was populated.
It may be unrelated to the OP a little bit, but I ended up here looking for an answer.
<link *ngIf="cssUrl" rel="stylesheet" type="text/css" [href]="sanitizer.bypassSecurityTrustResourceUrl(cssUrl)">
回答29:
I met this issue.
I refused to apply the style from 'http://m.b2b-v2-pre1.jcloudec.com/mobile-dynamic-load-component-view/resource/js/resource/js/need/layer.css?2.0' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Changing the path could solve this issue.
回答30:
I had the same problem, and in my case it was due to the fact that I had manually tried to import (s)CSS files in the HTML file (like we always do with static websites), when in fact the files had to be imported in the entry point JavaScript file used by Webpack.
When the stylesheets were imported into the main JavaScript file, rather than manually written in the HTML, it all worked as expected.
来源:https://stackoverflow.com/questions/48248832/stylesheet-not-loaded-because-of-mime-type