【问题标题】:Rails serving .otf file with wrong MIME type. application/vnd.oasis.opendocument.formula-templateRails 为 MIME 类型错误的 .otf 文件提供服务。应用程序/vnd.oasis.opendocument.formula-template
【发布时间】:2013-05-24 22:02:03
【问题描述】:

这是我所做的:

首先我在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;
}

我使用的是谷歌浏览器,控制台显示:

Resource interpreted as Font but transferred with MIME type
application/vnd.oasis.opendocument.formula-template: 
"http://localhost:3000/assets/ArnoPro-Display.otf".

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

【问题讨论】:

    标签: ruby-on-rails sass font-face mime-types ruby-on-rails-4


    【解决方案1】:

    更新

    解决方案 1

    application/x-font-opentype 更改为 font/opentype 。来源:font mime type

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

    清除缓存

    rake tmp:cache:clear
    

    在重新启动服务器之前。


    如果解决方案 1 不起作用,您应该创建文件 .htaccess 并添加此文件

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

    【讨论】:

    • 更改它,重新启动我的服务器并遇到同样的问题。还有其他想法吗?
    • 更新答案,如果不行,您可以创建.htaccess文件并添加AddType application/vnd.oasis.opendocument.formula-template .otf
    • 您的编辑不会将 MIME 类型 设置为 vnd.oasis.opendocument.formula-template 吗?我试图阻止它具有那种 mime 类型,而不是鼓励它。
    • 您的第三次更新(添加src: url(/assets/ArnoPro-Display.otf);)不起作用,因为字体的实际网址变为assets/assets/ArnoPro-Display.otf。请记住,我在资产管道中注册了该文件夹。
    • @Serg :对不起,Serg,我已经清理并更新了我的答案。更改mime_type.rb 后,您应该在重新启动之前wipe out the cache。希望对您有所帮助。
    猜你喜欢
    • 2011-06-23
    • 2021-05-08
    • 1970-01-01
    • 2015-09-26
    • 2021-08-27
    • 2018-11-28
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多