【问题标题】:Rails 4.0.13 assets url missing prefix and digestRails 4.0.13 资产 url 缺少前缀和摘要
【发布时间】:2015-12-06 19:54:42
【问题描述】:

我已经开始开发 Rails 4.0.13 应用程序,我们正在尝试将资产移动到 CDN

问题出现在我们的暂存服务器上,生成的 url 缺少前缀和摘要

https://xxyyzz.blob.core.windows.net/stylesheets/application.css

在我的本地机器上使用与登台服务器上相同的环境,一切正常

https://xxyyzz.blob.core.windows.net/c532aef6b12d99b3ce3f05b4fc17c02d8a682b13/application-dd1ee03f18e6ee4df65b6a21d8cfcdeb.css

config/environments/dev_1.rb

...
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_assets = false
  config.assets.compile = false
  config.assets.digest = true
  config.assets.debug = false
  config.assets.version = '1.0'
  config.log_level = :info
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_tags = [CustomTagger::USER_TOKEN_LAMBDA, CustomTagger::PROCESS_PID]
  config.cache_store = :redis_store, RedisStores.new.url(db: 3), { expires_in: 1.day, driver: :hiredis }

config/application.rb

...
 config.assets.enabled = true

 config.assets.precompile = Proc.new do |filename, path|
   case
   # JS
   when /application-.+\.js/ =~ filename then true
   when filename == 'vendor.js' then true
   # CSS
   when filename.end_with?('master-default.css') then true
   when filename == 'application.css' then true
   # IMG
   when /assets\/images\// =~ path then true
   # Exclude everything else.
   else false
   end
 end
 config.asset_host = 'https://xxyyzz.blob.core.windows.net'

 # Each environment must have a different asset path (the SHA1 digest of
 # Rails.env + Rails.application.revision).
 #
 # See http://guides.rubyonrails.org/configuring.html#rails-general-configuration.
 # See https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L174-L186
 config.after_initialize do
   ActiveSupport.on_load(:action_view) do
     self.assets_prefix = Digest::SHA1.hexdigest(Rails.env + Rails.application.revision)
   end unless Rails.env.development?
 end
 config.autoload_paths += %W(#{config.root}/lib)
 config.middleware.insert_before Rack::Sendfile, 'HttpMethodNotAllowed'

有没有其他人遇到过这种情况?

【问题讨论】:

  • config/environments/dev_1.rb 是您的登台服务器的环境文件吗?我假设是这样,但是您提到了两种环境,但是您的帖子仅包含一个文件(其名称与任何一个都不匹配)。我想只要你有config.assets.digest = true,你就应该得到指纹。
  • 是的 dev_1 是使用的环境。我不知道您对 2 个环境的印象是什么。我还认为 config.assets.digest = true 应该添加指纹...
  • 抱歉,我错过了这是一个环境,并且在本地和远程服务器上的工作方式不同。听起来像是部署问题...您确定本地服务器上的所有内容都在您的存储库中并且已按预期部署到远程服务器吗?
  • 是的,我添加了记录器来检查配置/环境值,一切似乎都一样。还从 Rails 控制台检查。

标签: ruby-on-rails url ruby-on-rails-4 assets digest


【解决方案1】:

找出问题所在https://github.com/rails/rails/issues/15873

似乎 rails 会检查文件是否存在于 public/assets 中,如果不存在,则不会设置前缀和摘要。

类似问题:https://serverfault.com/questions/638905/does-rails-4-asset-path-helper-uses-asset-prefix

一个可能的解决方案是将 manifest.json 文件保存在 repo 中 或添加仅预编译清单文件的部署任务。 https://github.com/rails/sprockets-rails/issues/107

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多