Rails 4.0.13 资产 url 缺少前缀和摘要
Rails 4.0.13 assets url missing prefix and digest
我已经开始开发 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'
有没有其他人遇到过这个?
找出问题所在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
我已经开始开发 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'
有没有其他人遇到过这个?
找出问题所在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