rake assets:precompile taking extremely long to complete

允我心安 提交于 2019-11-29 01:32:54

I am on Rails 3.2.13 - I had the same problem with css compression taking an extremely long time. To fix:

In Gemfile add:

gem 'yui-compressor'

In config/environments/production.rb:

config.assets.css_compressor = :yui
config.assets.js_compressor = :yui

rake assets:precompile without those changes: 325 seconds

rake assets:precompile with those changes: 79 seconds

rake assets:precompile with no compression: 45 seconds

The hackety hack solution seems to be to monkey patch the standard sass compression engine out of the way. I added this to the top of my application.rb

module Sass
  module Rails
    class CssCompressor
      def compress(css)
        css
      end
    end
  end
end

The difference in file size was 124k before the monkey patch and 125k after and an order of magnitude speed improvement.

The best option is compile locally, commit and deploy as normal, disabling the precompile task for production. I am doing this for all my production apps now.

To get around those compiled assets being served in Development mode (overriding dynamic pipeline compilation, which you need) do the following.

In development.rb place the following line:

config.assets.prefix = "/dev-assets"

This over-rides whatever is set in application.rb (normally "/assets").

You will also need this in application.rb:

config.assets.initialize_on_precompile = false

That stops the task trying to connect to your database. (Beware if you are referring to ActiveRecord models in your assets, as this won't work).

These changes allow you to compile and commit the assets to your repository locally, and have those files in your working development tree, but for development requests to still be sent to Sprockets. Plus, you only have to precompile and commit when something has actually changed.

Ref my blog post

We had orders of magnitude speedup using this answer:

EXECJS_RUNTIME='Node' JRUBY_OPTS="-J-d32 -X-C" RAILS_ENV=development bundle exec rake war

http://avinmathew.com/improving-rails-asset-precompile-times-on-jruby/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!