【问题标题】:precompile specific assets without md5 fingerprint预编译没有 md5 指纹的特定资产
【发布时间】:2013-10-18 22:25:47
【问题描述】:

在我的 rails 4 项目中,css 使用字体文件。所以需要额外预编译。

我通过在 config/environments/production.rb 中添加以下行来实现这一点

  # Add the fonts path
  config.assets.paths << Rails.root.join('vendor', 'assets', 'fonts','fonts')
  #
  # # Precompile additional assets
  config.assets.precompile += %w( *.svg *.eot *.woff *.ttf )

并运行 rake assets:在生产环境中预编译。

但是,结果如下:

I, [2013-10-10T19:27:51.931963 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-ab2f6984951c07fd89e6afdefabd93c7.eot
I, [2013-10-10T19:27:51.940615 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-24dfb40c91db789b8b8faba6886ac1ef.svg
I, [2013-10-10T19:27:51.950685 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-4b2130768da98222338d1519f9179528.ttf
I, [2013-10-10T19:27:51.983230 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-7a07f26f72466361ac9671de2d33fd1c.woff

和 css 文件是指没有这个 md5 指纹的字体文件。

如何预编译资产,以便在没有 md5 指纹的情况下生成它们?还是应该在这种情况下将它们放到 public/fonts/ 文件夹中?

【问题讨论】:

  • 你解决了吗?我有同样的问题..
  • 我的回答有效吗?

标签: ruby-on-rails assets precompile


【解决方案1】:

按照这些步骤进行

  • 你的字体必须是app/assets/fonts
  • 将您的字体添加到资产路径(就像您所做的那样),但更喜欢config/application.rb
  • 在 CSS 中使用 à @font-face 声明字体。你会发现some help here
  • 如果您不使用 SCSS,则必须嵌入像 application.css.erb 这样的 css 并使用 asset_path() 助手在您的字体声明中实现您的字体路径。

一个没有 SCSS 的例子:

@font-face {
  font-family: 'MyFont';
  src:url('<%= asset_path("myfont.eot")%>');
  src:url('<%= asset_path("myfont.eot?#iefix")%>') format('embedded-opentype'),
    url('<%= asset_path("myfont.svg#myfont")%>') format('svg'),
    url('<%= asset_path("myfont.woff")%>') format('woff'),
    url('<%= asset_path("myfont.ttf")%>') format('truetype');
  font-weight: normal;
  font-style: normal;
}

【讨论】:

  • 问题不在于如何创建字体,而是在为生产字体创建预编译资产时出现问题,而是使用 md5 指纹。但在网络控制台后出现 fontawesome-webfont-62877.woff 404 错误。看起来 rails 在 publick/assets fontawesome-webfont-62877-e70f92449ebfddada3d455eb44542655.woff 中看不到预编译资产,但是当我在没有 md5 的情况下添加文件 fontawesome-webfont-62877.woff 时,它工作正常。如何创建让 rails 在生产环境中看到字体的预编译。
  • @SergeyChechaev 用我的方法,一切都在生产模式下工作。使用 rails3 我遇到了问题,解决方案是在 public/assets/ 中复制字体目录。如果您不想预编译字体,为什么要将它们添加到资产系统中?有点奇怪:)
  • 我不明白如何在生产模式下预编译资产,这是 fork 很好我有 404 错误。
  • @SergeyChechaev 仔细阅读了答案 - 您需要将 (S)CSS 文件更改为具有 .erb 扩展名,以便在预编译时包含指纹字体文件路径而不是原始文件名。
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 2023-03-09
  • 2012-01-09
  • 2016-05-26
  • 2015-10-31
相关资源
最近更新 更多