【问题标题】:Rails asset pipeline: custom font does not load (hash not appended to file name in compiled CSS)Rails 资产管道:自定义字体未加载(哈希未附加到已编译 CSS 中的文件名)
【发布时间】:2019-10-02 06:33:41
【问题描述】:

我有以下 SASS 代码来加载自定义字体:

@font-face
  font-family: 'Fredericka the Great'
  font-style: normal
  font-weight: 400
  src: local('Fredericka the Great'), local('FrederickatheGreat'), asset-url('assets/fredericka_the_great.woff2') format('woff2')
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215

在开发中,它可以正常加载。

在我的一台服务器上,它没有。仔细观察生成的代码,我注意到在编译后的 CSS 中,应该加载 woff 文件的位置没有附加哈希:

@font-face{
  font-family:"Fredericka the Great";
  font-style:normal;
  font-weight:400;
  src:local("Fredericka the Great"),local("FrederickatheGreat"),url(/assets/fredericka_the_great.woff2) format("woff2");
  unicode-range:U+0000-00FF,U+0131,U+0152-0153,U+02C6,U+02DA,U+02DC,U+2000-206F,U+2074,U+20AC,U+2212,U+2215}

如您所见,编译后的代码生成url(/assets/fredericka_the_great.woff2),而在服务器上,部署的资产如下所示:

所以在我看来,CSS 应该产生url(/assets/fredericka_the_great-49badd2a1e856a1d9e3a6cb6189f0426bda9f2b292376395758419c2d0c631b6.woff2)。事实上,尝试手动加载文件是可行的:

有趣的是,在其他服务器上,相同的应用程序按预期工作(这看起来很奇怪,因为那里的 CSS 也缺少哈希)!

(更详细的 CSS 检查,请转到 https://audit.uber.space/assets/application-9e5ba30be64b818da5468c9b39dc20aafa0ce523315d3e6684b96d11e336f603.css - 或者,有趣的是,只需 https://audit.uber.space/assets/application.css)

让我非常怀疑的是,当我将服务器上的 woff 文件重命名为完全不同的名称时,网站仍在加载该文件。所以可能服务器甚至没有发送编译后的 woff 文件,而是一些完全不同的 woff 文件(可能直接来自 gem 目录或其他东西)......?

我现在不确定如何继续,因为我不知道预期的行为是什么,以及如何修复我的部署。

为了完整起见,这是我的部署配置(我使用 mina):

$ mina -d

------- Printing current config variables -------
:debug_configuration_variables => true
:port => 22
:releases_path => #<Proc:0x007fc881157470@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/deploy.rb:3 (lambda)>
:shared_path => #<Proc:0x007fc881157380@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/deploy.rb:4 (lambda)>
:current_path => #<Proc:0x007fc8811572b8@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/deploy.rb:5 (lambda)>
:lock_file => "deploy.lock"
:deploy_script => "data/deploy.sh.erb"
:keep_releases => 5
:version_scheme => :sequence
:execution_mode => :pretty
:bundle_bin => "bundle"
:bundle_path => "vendor/bundle"
:bundle_withouts => "development test"
:bundle_options => #<Proc:0x007fc8802da898@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/bundler.rb:6 (lambda)>
:shared_dirs => ["vendor/bundle", "log", "tmp/cache", "public/assets", "public/uploads", "tmp/sockets", "tmp/pids"]
:rails_env => "production"
:bundle_prefix => #<Proc:0x007fc8802cbeb0@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/rails.rb:5 (lambda)>
:rake => #<Proc:0x007fc8802bbb50@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/rails.rb:6 (lambda)>
:rails => #<Proc:0x007fc8802bb970@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/rails.rb:7 (lambda)>
:compiled_asset_path => "public/assets"
:asset_dirs => ["vendor/assets/", "app/assets/"]
:migration_dirs => ["db/migrate"]
:branch => "master-nine"
:remove_git_dir => true
:remote => "origin"
:git_not_pushed_message => #<Proc:0x007fc88021ab10@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-1.2.3/tasks/mina/git.rb:6 (lambda)>
:web_server => :puma
:puma_role => #<Proc:0x007fc8800e7a90@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:7 (lambda)>
:puma_env => #<Proc:0x007fc8800e77c0@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:8 (lambda)>
:puma_config => #<Proc:0x007fc8800e7590@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:9 (lambda)>
:puma_socket => #<Proc:0x007fc8800e73d8@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:10 (lambda)>
:puma_state => #<Proc:0x007fc8800e7248@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:11 (lambda)>
:puma_pid => #<Proc:0x007fc8800e7108@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:12 (lambda)>
:puma_cmd => #<Proc:0x007fc8800e6e10@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:13 (lambda)>
:pumactl_cmd => #<Proc:0x007fc8800e6d20@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:14 (lambda)>
:pumactl_socket => #<Proc:0x007fc8800e6988@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:15 (lambda)>
:puma_root_path => #<Proc:0x007fc8800e65c8@/Users/josh/.rvm/gems/ruby-2.4.1@XXX/gems/mina-puma-1.1.0/lib/mina/puma/tasks.rake:16 (lambda)>
:rvm_use_path => "$HOME/.rvm/scripts/rvm"
:application_name => "A4AA 2.0"
:domain => "zugangfueralle01.nine.ch"
:deploy_to => "XXX"
:repository => "XXX"
:user => "www-data"
:forward_agent => true
:puma_port => 3006
:shared_files => ["config/secrets.yml"]

【问题讨论】:

    标签: ruby-on-rails deployment asset-pipeline custom-font


    【解决方案1】:

    几个月前我遇到了同样的问题,而你的情况非常多。这是我解决它的方式:

    1.- 路线,第一件事就是第一。 最好的方法是保存到app -&gt; assets -&gt; fonts (You create this folder)。然后我建议以可读的方式添加你的字体文件(.woff)。

    2.- SASS 我特别需要将此代码添加到我的 application.scss 或 custom.scss

    @font-face {
        font-family: 'Theola Kids Regular';
        font-style: normal;
        font-weight: normal;
        src:font-url('theola-kids.woff');
        src:font-url('theola-kids.woff') format('woff');
    } 
    

    注意到src:font-url,它告诉rails 去寻找一个特定的字体文件。我有两个作为路由,另一个作为路由格式文件。

    3.- 管道

    务必将“字体”添加到 application.rb 文件中 示例:config.assets.paths &lt;&lt; Rails.root.join("app", "assets", "fonts") 完成后重新启动您的 Rails 服务器并检查它。它必须与此解决方案一起使用。

    有关更多详细信息,我强烈推荐这篇文章: https://gist.github.com/anotheruiguy/7379570

    祝你好运!

    【讨论】:

    • 嘿,Ken,这很好用,非常感谢。还有一个问题:I have two one as a route and the other a route format file. 是什么意思?顺便说一句,我在 Bootstrap-Sass 上添加了一个后续问题:Fonts are not included correctly。
    • 而且看来config.assets.paths &lt;&lt; "fonts"(没有Rails.root.join ...)就够了。
    • 回答你的第一个问题:src:font-url('theola-kids.woff'); &lt;--the normal route of the file src:font-url('theola-kids.woff') format('woff');
    • 响应您的第二个请求,是的,您可以通过不同的方式将字体扩展添加到管道中。我个人发现地雷更好,更容易理解。但是你知道,这取决于我们每个人你如何理解它。很高兴它有帮助!快乐编码:D
    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 2016-06-06
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2012-10-01
    相关资源
    最近更新 更多