Slow Assets Compilation in Development Mode

Slow assets compilation in development mode

It's a sad truth, but you don't. There is not a clean way to solve this.

However, there are some patterns you could follow to minimize your pain which if I understand correctly is having to wait a lot in development to see the changes.

As said these have been seen here1 and here2.

  1. Take a look at item 2 from here1.
  2. Break your assets in many files. This will imply in fewer lines being processed when changes occur.
  3. Prefer css/js, they may not be as cool but require no compilation.
  4. Find something interesting to do while assets precompile. It may lower productivity but sure kills the pain.

Rails Asset Pipeline/Compass/SASS extremely slow to compile in development mode

Turns out there is an issue as pointed out by fredwu on the rails github:

I believe it's an issue with Sprockets' resolver:
https://github.com/sstephenson/sprockets/blob/v2.2.2/lib/sprockets/trail.rb#L70-L83

Rails .12 resolves the asset paths manually whereas .13 tries to use
Sprockets resolver, which then throws an exception.

The interim issue is to use the following line in your gemfile:

gem 'rails', git: "git://github.com/rails/rails.git", branch: "3-2-stable"

https://github.com/rails/rails/issues/9803

Rails Minified (Compiled) Assets in development mode

That's quite simple.

You only need to add/change the following line in your config/environments/development.rb

config.assets.debug = false

And restart your rails server.

Assets loading slowly in development

Turning off asset debugging should improve the speed a bit.

#development.rb
config.assets.debug = false

rake assets:precompile is slow

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.

Rails server start up time is very slow with upgrade

Thanks for pitching in folks. Just now, got it resolved.
I am using turbo-sprockets-rails-4 in which preload assets is enabled by default for dev environments.

added below config in config/environments/development.rb

TurboSprockets.configure do |config|
config.preloader.enabled = false
end

now server startup time is as fast as rails3

this is mentioned in their readme itself, but missed it while skimming through

How can you speed up the Rails Asset Pipeline precompile process?

1. Capistrano deployment speedup

(1) use capistrano built-in task 'deploy/assets' to deploy.

Capistrano has its own built-in task 'deploy/assets'. It will automatically do task for you.

The difference between your own handcraft task is it only load assets group to precompile assets, not whole environment.

cd /home/apps/APP_NAME/releases/20120708184757 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile

(2) skip precompile process when assets aren't changed.

https://gist.github.com/3072362

If

  • app/assets
  • lib/assets
  • vendor/assets
  • Gemfile.lock
  • confir/routes.rb

are changed, it will recompile assets. Otherwise, it will skip the pecompile process, save a lot of time.

2. Use @import carefully.

(1) avoid using @import "compass"; directly.

It will both work when you

@import "compass"; or @import "compass/typography/links/link-colors"; in SCSS.

But @import "compass/typography/links/link-colors"; is 9 times faster than @import "compass"; when you compile assets.

That is because when @import "compass";, it compile whole compass assets. not only just link-colors part.

(2) avoid using partials

In SCSS, we like to use partial to organize our assets.

But only if you need to share variables, or there are necessary dependencies, otherwise

//= require "reset"
//= require "base"
//= require "product"

is faster than

@import "reset";
@import "base";
@import "product";

3. don’t require .scss & .coffee for no reason

(1) avoid using require_tree

When we use Rails generator to generate controllers. Rails will also generate assets likes this

  • product.css.scss
  • product.js.coffee

and mount assets in application.js using this techniques:

//= require_tree

But the empty assets (output nothing) which only contain this lines:

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

It will cost you about 250ms to compile each of them. If you have 10 empty assets, it will be 2.5 seconds .

Remove them from your project, or mount them individually in application.js like this:

//= require prodcuts
//= require users
//= require albums

(2) Don't use css.scss or js.coffee if unnecessary.

  • Compiled jquery-ui-1.8.16.custom.css (0ms) (pid 19108)
  • Compiled jquery.ui.1.8.16.ie.css (0ms) (pid 19108)
  • Compiled jquery.js (5ms) (pid 19108)
  • Compiled jquery_ujs.js (0ms) (pid 19108)
  • Compiled custom.css (14ms) (pid 19108)

custom.css is custom.css.scss

Compile pure CSS and pure JS is fast ( cost almost 0 ms). But compile .scss and .coffee still cost some time.

Summarize

  1. replace deploy.rb assets task.
  2. check logs/production.log

    • find slow assets
    • remove @import "compass"; use alternative solution.
    • use require instead @import; ( use @import when it is really necessary )
    • remove require_tree, mount assets individually
    • remove empty .scss and .coffeescript
    • use .css when assets are pure CSS.


Related Topics



Leave a reply



Submit