【问题标题】:Rails not using digested assets in productionRails 在生产中不使用消化的资产
【发布时间】:2015-12-04 23:38:26
【问题描述】:

我有一个 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 上运行时,显然有些不同,但配置文件的设置是相同的,gem 等也是如此。这是我们的 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,确实解决了问题,但也会导致服务器在部署后尝试加载页面超时。

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    部署后您可能需要在生产环境中运行预编译任务。

    我假设 Heroku 默认会这样做。

    这里有几个例子:

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

    【讨论】:

    • 感谢您的想法,但我们已经按照此处的建议在 before_release 任务中这样做了 support.aptible.com/topics/paas/how-to-store-static-assets
    • 原来 aptible 支持文章是错误的,我确实需要在 Dockerfile 中执行预编译资产命令,以确保它发生在同一个容器内。
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2015-02-23
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多