在Heroku上升级Rails 3.1时的资产管道

5

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

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

如果我正确理解了pipeline,那么每次从浏览器发出请求时,这个应该不会“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。 - Michael A.
4个回答

5

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

为了更快的资源预编译,您可以在config/application.rb中将config.assets.initialize_on_precompile设置为false,部分加载应用程序。但是在这种情况下,模板无法访问应用程序对象或方法。Heroku要求此项设置为false


4
我也在想同样的问题,但是这里有一个技巧可以帮助你确定你的资产是否正在实时编译:
  1. 在本地运行 rake assets:precompile
  2. 更改你的 css 但不要重新运行 rake 任务
  3. 添加、提交并推送到 heroku

如果您在第2步中所做的更改显示在 Heroku 上,则知道您的应用正在实时编译

不要忘记 Celadon 不再包括 Varnish,因此现在您需要负责 HTTP 缓存,所以您需要自己设置 rack-cache 和 memcached:

但是,我也觉得这很令人困惑。


1

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

config.action_dispatch.x_sendfile_header = "X-Sendfile"

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

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


“-----> 准备应用程序进行Rails资产管道处理” 出现了,但之前也有出现过。尽管如此,仍然会出现缓存未命中的情况... - alvatar
它不应该使用ngnix指令吗?config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' - tibbon

1

3
是的,我有Cedar堆栈。如果您启用了“实时编译”,它会在缓存未命中时每次编译它。实际上,如果我使用“config.assets.compile = true”选项停用此选项,则根本不起作用。 - alvatar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接