Rails提供.otf文件的MIME类型不正确。应该是application/vnd.oasis.opendocument.formula-template。

3

这是我所做的:

首先,在app/assets目录下创建了一个fonts文件夹。


然后,配置资产管道以识别这个新文件夹。

config/environments/development.rb

# Add the fonts path
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')

# Precompile additional assets
config.assets.precompile += %w( .svg .eot .woff .ttf .otf )

接下来,我在config/initializers/mime_types.rb中配置mime类型:
# Be sure to restart your server when you modify this file.

# Here are some example that came with the default Rails project.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone

Rack::Mime::MIME_TYPES['.otf'] = 'application/x-font-opentype'

最后,我在我的 SCSS 文件中引用字体:

@font-face {
  font-family: 'ArnoProDisplay';
  src: url('ArnoPro-Display.otf');
  font-weight: normal;
  font-style: normal;
}

我正在使用Google Chrome浏览器,控制台显示如下信息:
Resource interpreted as Font but transferred with MIME type
application/vnd.oasis.opendocument.formula-template: 
"http://localhost:3000/assets/ArnoPro-Display.otf".

我可以在网络选项卡中正确地看到字体:

enter image description here

1个回答

9

更新

解决方案1

application/x-font-opentype 更改为 font/opentype。来源:字体MIME类型

Rack :: Mime :: MIME_TYPES ['。otf'] ='font/opentype'

并清除缓存

rake tmp:cache:clear

在重启服务器之前,请注意以下事项。


如果解决方案1无效,则应创建文件.htaccess并添加以下内容:

AddType application/vnd.oasis.opendocument.formula-template .otf


更改了它,重新启动了我的服务器,但仍然遇到同样的问题。还有其他的想法吗? - sergserg
如果更新的答案不起作用,您可以创建一个.htaccess文件,并添加AddType application/vnd.oasis.opendocument.formula-template .otf - rails_id
你的编辑会将MIME类型设置为vnd.oasis.opendocument.formula-template吗?我正试图防止它具有该MIME类型,而不是鼓励它。 - sergserg
你的第三次更新(添加 src: url(/assets/ArnoPro-Display.otf);)无法正常工作,因为字体的实际 URL 变成了 assets/assets/ArnoPro-Display.otf。请记住,我已经在资产管道中注册了该文件夹。 - sergserg
@Serg:抱歉Serg,我已经清理并更新了我的答案。在更改mime_type.rb之后,您应该在重新启动之前清除缓存。希望这可以帮助您。 - rails_id
解决方案是按照您上面所说的设置MIME类型,还要使用Rake清除缓存。 - sergserg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接