Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake assets:precompile is slow

Tags:

The command "rake assets:precompile" works very slow for me. Especially on my Amazon EC2 Micro production server which does not have a lot of processor resources. On EC2 I have to wait 1 minute or more during each deployment just for this precompile task alone. Is there a way to make it faster?

Previously I used Jammit to compress/minify css and js. Jammit worked nearly 10 times faster on the same web site and servers.

like image 677
Evgenii Avatar asked Sep 24 '11 06:09

Evgenii


People also ask

What does rake assets Precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.

How do you Precompile assets Rails in production?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is asset pipeline Rails?

1 What is the Asset Pipeline? 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

If you don't need to load the Rails environment, you should disable that with:

config.assets.initialize_on_precompile = false 

EDIT: I've just written a gem to solve this problem, called turbo-sprockets-rails3. It speeds up your assets:precompile by only recompiling changed files, and only compiling once to generate all assets.

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 60
ndbroadbent Avatar answered Oct 23 '22 04:10

ndbroadbent


There is a bug in Rails 3.1.0 that includes too many files in the precompile process. This could be the reason for the slowness if you have many assets js and css assets.

The other is that Sprockets (the gem doing the compilation) is more complex and has to allow for more options - scss, coffeescript and erb. Because of this I suspect it will be slower doing just concatenation and minification.

As suggested, you could precompile the files before deploying them if this is still an issue.

like image 35
Richard Hulse Avatar answered Oct 23 '22 04:10

Richard Hulse