这个月,我从 Rails 3.0 升级到 Rails 3.1 - 这周我尝试以生产模式启动服务器 - 今天我碰壁了!
我无法让我的生产环境服务器通过资产管道提供我的公共资产(JavaScript 和 CSS),除非我config.assets.compile = true
在 environment.rb 文件中进行设置,出于速度原因,我显然不想这样做。
我有大量的 JS 和 CSS 文件,每个文件都倾向于在一两个不同的页面上使用。这意味着创建单个“清单”文件不适合我的用法,因为每个页面都需要稍微不同的东西。我还希望某些 CSS 不能很好地融合在一起。因此,我采取了“让它工作”的方法,希望以后整理大量的 CSS / JS。
这是 production.rb 文件:
Implicit::Application.configure do
...
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# I set this to true, as I am testing this locally, just running a local Thin server
config.serve_static_assets = true
config.assets.compress = true
# Setting this to false removes the issue - but is SLOW
config.assets.compile = true
config.assets.digest = true
# This is overkill - but does get EVERYTHING precompiled for now
config.assets.precompile += %w( *.css *.js )
config.action_dispatch.x_sendfile_header = nil
...
end
这对我来说是一个相当新的领域,所以我今天花了很多时间来切换这些布尔值并停止/启动本地 Thin / Rails 服务器来尝试它们。但是唯一产生明显差异的值是编译标志。
我的 application.rb 文件几乎是标准的,并且config.assets.enabled = true
其中包含config.assets.initialize_on_precompile = false
来自 heroku 帖子的后者(再次猜测)。
我有一个完全填充的public/assets
目录,使用该bundle exec rake assets:precompile
命令创建,在我的旧笔记本电脑(5 年)上运行大约需要 20 分钟,可能与“catch all”预编译正则表达式有关,尽管该行注释它仍然接管10 分钟 (!)
将 compile 标志设置为true
,我可以看到在我的/tmp/cache
目录中创建了这些资产的副本 - 这显然是创建它自己的资产“编译副本”的应用程序。
将编译标志设置为false
时,我会遇到jquery.reveal isn't precompiled
. 但是,当我转到http://localhost:3000/assets/jquery.reveal.js
javascript 文件时,它会提供服务。
导致此问题的应用程序布局行是:
<%= javascript_include_tag "application", "jquery.reveal" %>
我已经尝试将 jquery.reveal 更改为jquery.reveal.js
没有更改。删除它修复了索引页面,除了 jquery.reveal 功能当然消失了!很明显,application.js 得到了正确的服务。我只是不知道为什么 jquery.reveal 不是,因为我可以在 public/assets 目录中看到预编译的文件。