Rails在生产中不使用消化资产

时间:2015-12-04 23:38:26

标签: ruby-on-rails ruby-on-rails-4 asset-pipeline

我有一个Rails 4.2应用程序,它运行在Heroku上的预编译资产。我们正在尝试迁移到新平台(Aptible),但使用相同的设置,我们的应用程序不再正确获取资产。是的,我运行bundle exec rake assets:precompile来预编译资产并验证它们是否可用。

我让服务器打印出几个值

- puts Rails.application.assets.find_asset('application.css').digest_path
- puts stylesheet_link_tag 'application'

并且它具有正确的资产价值,但stylesheet_link_tag会生成错误的链接。

[web0] application-2c5efa873b0d0254861e6a7ee25995dd.css
[web0] <link rel="stylesheet" media="screen" href="https://<something>.cloudfront.net/stylesheets/application.css" />

显然,当我们在Heroku上运行时会有一些不同的东西,但是配置文件的设置与gems一样。以下是我们的staging.rb配置文件的相关部分

App::Application.configure do    
  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local = true
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_files = true
  config.static_cache_control = 'public, max-age=2592000'

  # Compress JavaScripts and CSS.
  # config.assets.css_compressor = :sass
  config.assets.js_compressor = :uglifier
  config.assets.js_compressor = Uglifier.new(mangle: false)
  config.assets.compile = false # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.digest = true # Generate digests for assets URLs.
  config.assets.version = '2.0' # Version of your assets, change this if you want to expire all your assets.

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  config.force_ssl = true

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = "https://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
  config.action_mailer.asset_host = config.action_controller.asset_host = 'https://<something>.cloudfront.net'
end

我在线搜索,但我发现的唯一建议是设置    config.assets.compile = true,它可以解决问题,但也会导致服务器在部署后超时尝试加载页面。

有谁知道这里有什么问题吗?为什么要寻找非消化资产?

谢谢!

1 个答案:

答案 0 :(得分:1)

部署后,您可能需要在生产时运行预编译任务。

我假设Heroku默认这样做。

这里有一些例子:

https://gist.github.com/fancyremarker/9652688