在本地开发人员上禁用Chrome严格的MIME类型检查 [英] disable chrome strict MIME type checking on local dev

查看:615
本文介绍了在本地开发人员上禁用Chrome严格的MIME类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSS文件,表明我的本地开发服务器(webpack)使用的是错误的mime类型.

I have a CSS file that my local dev server (webpack) is serving up with apparently the wrong mime type.

Refused to apply style from 'http://localhost:10001/font-awesome/css/font-awesome.min.css' 
because its MIME type ('text/html') is not a supported stylesheet MIME type, 
and strict MIME checking is enabled.

是否有禁用此功能?我认为这是一个镀铬设置.至少对于特定主机.

Is there anyway to disable this? I assume it's a chrome setting. At least for a specific host.

如果没有做类似这样的基本操作,则进入webpack配置通常是挫败牛剃毛的一种方法.

Digging into the webpack config, if it doesn't do something basic like this is usually an exercise in frustrating yak shaving.

大多数其他答案都涉及修复服务器的方法.我只是想破解此客户端,因为服务器很固执.

Most other answers refer to ways of fixing the server. I just want to hack this client side since the server is being stubborn.

相关:

  • Disable Chrome strict MIME type checking
  • Link and execute external JavaScript file hosted on GitHub

推荐答案

您的问题是前一段时间的,但是在此问题上在Google上排名很高,因此我想提出一些建议.

Your question is from a while ago, but ranks well on Google for this problem so I wanted to chip in with a couple pointers.

可能是伪装的HTTP/404

该错误消息可能只是在误导您. 不幸的是,谷歌浏览器彻底删除了响应,甚至删除了网络"开发人员面板的预览.

The error message is probably just misleading you. It's unfortunate that Google Chrome removes the response entirely, even the preview of the "Network" dev panel.

MIME类型可能不正确,因为服务器正在使用HTTP 404消息进行应答.
要测试:直接在浏览器中打开CSS文件的URL,或使用wget之类的工具获取该URL.

The MIME type might be incorrect because the server is answering with an HTTP 404 message.
To test: Open the URL to the CSS file in the browser directly, or fetch it with a tool like wget.

您检查路径是否正确吗?根据您的配置,css文件可能不像以前那样(假定webpack或其配置没有问题).

Have you checked if the path is correct? Depending on your configuration the css file might not be where it was before (Assuming it's not a problem with webpack or its configuration).

我只能在这里盲目猜测正确的URL是什么...
http://localhost:10001/node_modules/font-awesome/css/font-awesome.min.css
http://localhost:10001/css/font-awesome/font-awesome.min.css
http://localhost:10001/css/font-awesome.min.css
http://localhost:10001/font-awesome.min.css

I can just guess blindly here what the correct URL might be...
http://localhost:10001/node_modules/font-awesome/css/font-awesome.min.css
http://localhost:10001/css/font-awesome/font-awesome.min.css
http://localhost:10001/css/font-awesome.min.css
http://localhost:10001/font-awesome.min.css

检查请求标头

通常,不仅限于CSS响应:
如果通过text/html响应(例如,默认的Apache 404页面),则可以准备请求标头以适当地回退(例如).
Accept: text/css, text/plain, */*

In general, not just in case of CSS responses:
The request headers can be prepared to gracefully fallback, should a text/html response come through (the default Apache 404 page, for example).
Accept: text/css, text/plain, */*

但是您应该*/*配置为每个请求都可以接受.即使在本地开发环境中,它也不总是有用-错误类型的响应应尽早解决.

But you should not configure */* as acceptable for every request. It's not always useful, even on a local development environment - responses of the wrong type should be fixed early.

为什么框架要优雅地处理此问题

服务器可能会提供正确的答案,但是Content-Type标头不正确. 该框架假定服务器必须具有application/json",并允许JSON解析器对其进行处理.

The server may deliver the correct answer, but with an incorrect Content-Type header. The framework assumes the server "must have meant application/json" and lets the JSON parser to its thing anways.

如果这可行,则框架可以忽略不正确的Content-Type并仍然起作用.
此操作的主要目的是在共享主机环境中运行,在该环境中可能无法编辑.htaccess设置.
如果您的应用程序处理更严格的内容类型是可以的,但它会不时地调试此类错误.

If this works, the framework can just ignore the incorrect Content-Type and still function.
The goal of this is mainly to run on shared hosting environments where editing .htaccess settings may be impossible.
It's ok if your application handles content types more strict, with the drawback of debugging errors like this from time to time.

相关答案
如果这没有帮助,那么这里的答案对我很有帮助:
由于MIME类型而未加载样式表

Related answers
If this doesn't help, answers from here were helpful for me:
Stylesheet not loaded because of MIME-type

这篇关于在本地开发人员上禁用Chrome严格的MIME类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆