[英]Stylesheet not loading because MIME type is text/html
While running a MERN app on firefox, got this error in browser console and css is not loaded: The stylesheet http://localhost:3000/src/css/component.css was not loaded because its MIME type, “text/html”, is not “text/css”
在firefox上运行MERN应用程序时,在浏览器控制台中出现此错误并且未加载css:未加载The stylesheet http://localhost:3000/src/css/component.css was not loaded because its MIME type, “text/html”, is not “text/css”
I am adding CSS to index.html
like this : 我正在向index.html
添加CSS,如下所示:
<link rel="stylesheet" type="text/css" href="../src/css/component.css" />
Where am I going wrong ? 我哪里错了?
What are the other ways to add external css for sections apart from the React components ? 除了React组件之外,还为其他部分添加外部css的其他方法是什么?
The stylesheet http://localhost:3000/src/css/component.css was not loaded because its MIME type, “text/html”, is not “text/css” 未加载样式表http:// localhost:3000 / src / css / component.css ,因为其MIME类型“text / html”不是“text / css”
Reason for the error is, 错误的原因是,
you are allowed to access only public directory when its served on browser, so 在浏览器上提供服务时,您只能访问公共目录,所以
-> First ../src/css/
by this way you can't access the file , it will consider this as route and try to give you html - >首先../src/css/
通过这种方式你无法访问该文件,它会将此视为路由并尝试给你html
-> Second , This is not the proper way to include css files : - >其次,这不是包含css文件的正确方法:
<link rel="stylesheet" type="text/css" href="../src/css/normalize.css" />
<link rel="stylesheet" type="text/css" href="../src/css/demo.css" />
<link rel="stylesheet" type="text/css" href="../src/css/component.css" />
Proper way to use the css file is like this ( from your react component js file ) : 使用css文件的正确方法是这样的 (来自您的react组件js文件):
import './css/component.css';
( react-scripts start
) React will automatically convert your css file into js and applies it. ( react-scripts start
)React会自动将你的css文件转换成js并应用它。
Still if you want to use css files outside of react, you need to put all css files inside public folder (good to put inside public/css) 如果你想在反应之外使用css文件,你需要将所有css文件放在公共文件夹中(很好放在public / css中)
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/component.css" />
If you still have doubts please read : 如果您仍有疑问请阅读:
https://github.com/facebookincubator/create-react-app/blob/master/README.md#getting-started https://github.com/facebookincubator/create-react-app/blob/master/README.md#getting-started
Hope, this will clear all your doubts. 希望,这将清除你所有的疑虑。
Im using MiniCssExtractPlugin and i realized missing ' . 我使用MiniCssExtractPlugin,我意识到缺少' 。 ' will caused below issue. '会导致以下问题。
from 从
filename: '/style.[contenthash].css'
to 至
filename: './style.[contenthash].css'
//correct way should be like this new MiniCssExtractPlugin({ publicPath: './sass/', filename: './style.[contenthash].css' });
Hope this help you. 希望这对你有所帮助。
I moved the folder css and js to public directory and referenced the component.css
as 我将文件夹css和js移动到公共目录并将component.css
引用为
<link rel="stylesheet" type="text/css" href="%PUBLIC_URL%/css/component.css" />
It worked for me.. 它对我有用..
Any Better way ?? 有更好的方法??
You have to check that file exist on server. 您必须检查服务器上是否存在该文件。 I found the reason, because there are no file (or missing) when upload source code to the server. 我找到了原因,因为在将源代码上传到服务器时没有文件(或丢失)。 So when the css file not found, server returns html MIME type 因此,当找不到css文件时,服务器返回html MIME类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.