Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Rails 3 assets precompile faster?

Tags:

I have a running Rails 3.2.1 application, that I'm deploying via Capistrano with the deploy/assets which runs the deploy:assets:precompile task.

Everything works just fine, except that the compilation itself is really slow. I don't have that much CSS and JavaScript (about 8200 lines total).

It generally takes about 1-3 minutes to compile the assets, which is about 90% of the whole deployment time.

Is there any way to optimize this? Maybe use a different procedure to compile the assets or somehow optimize it?

I'm running the app on Linode 512 on 1.9.2-p290, Rails 3.2.1 and using therubyracer gem if that's of any relevance.

like image 247
Jakub Arnold Avatar asked Feb 15 '12 14:02

Jakub Arnold


People also ask

What does Rails assets Precompile do?

The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots. There are two ways you can use the asset pipeline on Heroku. Compiling assets locally.

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass, and ERB.


2 Answers

This isn't an answer on making it run faster, but as far as a "different procedure" goes, you can tell Capistrano to only precompile assets when you've actually made any changes to your assets. You would want to do a custom assets:precompile task something like this, which would look at the git logs between the existing and newly deployed code. For me, this worked great and now I only need to deal with slow deployment when updating assets:

namespace :deploy do   namespace :assets do     task :precompile, :roles => :web, :except => { :no_release => true } do       from = source.next_revision(current_revision)       if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0         run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}       else         logger.info "Skipping asset pre-compilation because there were no asset changes"       end     end   end end 

Source: http://www.bencurtis.com/2011/12/skipping-asset-compilation-with-capistrano/

like image 57
Dylan Markow Avatar answered Oct 26 '22 23:10

Dylan Markow


I've just written a gem to solve this problem inside Rails, called turbo-sprockets-rails3. It speeds up your assets:precompile by only recompiling changed files, and only compiling once to generate all assets. It works out of the box for Capistrano, since your assets directory is shared between releases.

It would be awesome if you could help me test out the turbo-sprockets-rails3 gem, and let me know if you have any problems.

like image 45
ndbroadbent Avatar answered Oct 26 '22 23:10

ndbroadbent