CSS 文件未被应用

CSS file is not being applied

我正在尝试使用 Buffalo 构建网络应用程序 但我很难弄清楚为什么我的 site.css 文件没有被应用。

我在 Chrome 上遇到的错误是:

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

我已经尝试通过 docs 查看 Buffalo 如何管理资产,它说:

Any assets placed in the /assets folder will be copied to the "distribution" automatically, and can be found at /assets/path/to/asset.

我的代码 link 样式表如下所示:

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

关于我还可能遗漏什么的任何想法?

谢谢!

这意味着您的服务器没有使用适当的 MIME 类型响应对此 css 文件的请求。您可以通过请求 CSS 文件并查看原始响应来证明这一点(例如,在浏览器的开发人员工具中,或通过 cURL)。你想看的是:

text/css

但是,鉴于错误,您将看到的是 text/plain

根据Buffalo documentation

By default the asset pipeline is configured to use .scss files, with /assets/css/application.scss as the main entry point. This, of course, can be changed.

我建议你采用默认方案 use SCSS, and use the Buffalo asset helper:

stylesheetTag - This helper will generate a style tag for the requested CSS file. Example: <%= stylesheetTag("application.css") %> would return something like <link href="/assets/application.bd76587ded82386f388f.css" media="screen" rel="stylesheet" />

但是,如果您不想这样做,您可以研究使用 Buffalo 提供 CSS 文件的替代配置,或者绕过 Buffalo 并在没有它的情况下提供 CSS 文件。