【问题标题】:Assets pipeline when updating to Rails 3.1 on Heroku在 Heroku 上更新到 Rails 3.1 时的资产管道
【发布时间】:2011-09-03 16:52:58
【问题描述】:

我刚刚将我在 Heroku 上的应用从 Rails 3.0 升级到 3.1,并且我正在尝试让资产管道正常工作。主要问题是我可以从 heroku 日志中为每个资产读取以下类型的行:

2011-09-03T16:35:28+00:00 app[web.1]: cache: [GET /assets/border-a3c571a354b9381740db48aeebfaa63a.jpg] miss

如果我对管道的理解正确,那么对于我从浏览器发出的每个请求,这不应该是“错过”,而是应该在缓存中找到。

阅读 Heroku 文档,您可以找到以下解释:

Rails 3.1 provides an assets:precompile rake task to allow the compilation penalty to be paid up front rather than each time the asset is requested. If this task exists in your app we will execute it when you push new code.

但是“assets:precompile”任务应该是怎样的呢?我尝试从头开始使用 rails 3.1 构建一个项目以试图找出答案,但在裸项目中没有这样的任务。还是我错过了什么?我怎样才能在缓存中找到资产?也许只是配置的问题。

这些是我的生产配置文件的选项:

config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = true # If I turn this off I get a 500 error and logs say that an  asset isn't compiled

我的 application.rb 有这一行:

config.assets.enabled = true

非常感谢您的帮助!

【问题讨论】:

  • 我直接在 heroku 上编译资产,不会用编译的资产污染我的本地存储库。使用命令:heroku run rake assets:precompile

标签: ruby-on-rails heroku ruby-on-rails-3.1 web-deployment


【解决方案1】:

另外,看看http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

为了更快地进行资产预编译,您可以部分加载您的应用程序 通过将 config.assets.initialize_on_precompile 设置为 false 在 config/application.rb,虽然在这种情况下模板看不到 应用程序对象或方法。 Heroku 要求这是错误的

【讨论】:

    【解决方案2】:

    我也想知道同样的事情,但这里有一个提示可以帮助您确定您的资产是否正在实时编译

    1. 在本地运行rake assets:precompile
    2. 对您的 css 进行一些更改,但 不要重新运行 rake 任务
    3. git add、commit 和 push 到 heroku

    如果您在步骤 2 中所做的更改显示在 heroku 上,那么您就知道您的应用正在实时编译

    别忘了你现在负责http缓存,因为varnish不再包含在青瓷中,所以你需要自己设置rack-cache和memcached:

    但是是的,我也觉得这很令人费解

    【讨论】:

      【解决方案3】:

      您可以尝试将config.serve_static_assets 设置为true

      config.action_dispatch.x_sendfile_header = "X-Sendfile"
      

      已添加到您的 config/environments/production.rb 文件中?

      当您将代码推送到 Heroku 时,您应该会看到 slug 编译器 AFAICT 宣布的预编译。

      【讨论】:

      • "-----> 为 Rails 资产管道准备应用程序" 出现,但之前也是如此。不过仍然会出现缓存未命中...
      • 不应该使用ngnix指令吗? config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
      【解决方案4】:

      确保您使用的是Heroku "Cedar" stack。然后Heroku will automatically precompile your assets during slug compilation

      注意:我仍然会遇到“缓存未命中”,但我认为这不是真的,因为如果您的资产未编译,您的应用将无法运行。

      【讨论】:

      • 是的,我有 Cedar 堆栈。如果您激活了“实时编译”,它会在每次缓存未命中时进行编译。实际上,如果我停用此选项(使用 'config.assets.compile = true'),那么它根本不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2011-11-10
      相关资源
      最近更新 更多