在Ruby on Rails 3中压缩资产

5

我在production.rb文件中有以下配置:

 # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Choose the compressors to use
  config.assets.js_compressor  = :uglifier
  config.assets.css_compressor = :yui

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Generate digests for assets URLs.
  config.assets.digest = true

但是当在生产服务器上运行Ruby on Rails应用程序时,我遇到了以下错误:

Error compiling CSS asset

LoadError: cannot load such file -- yui-compressor

在注释行LoadError: cannot load such file -- yui-compressor之后,一切都很顺利。
我需要一个压缩器,在发送资产之前对其进行gzip压缩以提高性能!
那么,问题出在哪里,或者是否有其他替代方案?



更新:
同样,JavaScript压缩也不起作用,因为Firefox的yslow插件还显示需要压缩JavaScript。

2个回答

8

你有包含在Gemfile中yui-compressor gem吗?如果没有,请将其包含进去,运行 'bundle install',然后Rails应该能够找到它。


是的,我已经在Gemfile中包含了yui-compressor gem,并运行了bundle install。但是irb也显示相同的错误。 - Chirag Rupani

3

我发现为了生效,Web服务器必须启用压缩
只需要添加以下内容:

AddOutputFilterByType DEFLATE text/html text/css application/x-javascript application/javascript

在我的情况下,针对Apache(Phusion Passenger)的<VirtualHost *:80></VirtualHost>指令。

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