10
votes

I have a large rails app with hundreds of coffee script files.

Sometimes when I make a tiny change in a coffeescript file or I switch the branch the whole assets are being precompiled and I have to wait a long time for load the page:

Started GET "/assets/application.js" for 127.0.0.1 at 2013-01-11 19:39:45 +0100
Compiled sprockets/commonjs.js  (0ms)  (pid 18142)
Compiled jquery.js  (2ms)  (pid 18142)
Compiled jquery_ujs.js  (0ms)  (pid 18142)
Compiled underscore.js  (0ms)  (pid 18142)
Compiled backbone.js  (0ms)  (pid 18142)
Compiled backbone_rails_sync.js  (0ms)  (pid 18142)
Compiled handlebars.runtime.js  (0ms)  (pid 18142)
Compiled moment.js  (0ms)  (pid 18142)
...and so on

I use the following assets configuration config/development.rb:

# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = false

When I set config.assets.debug = false I have to wait quite long time for load hundreds of js files. The question is: how to find the golden mean? How to optimize assets configuration in the development mode for the large app?

3
To be honest I don't trust any 3rd party libraries/workarounds. I'm looking for a clean rails way solution for handle cases like this this one.luacassus
Hundreds of js files x 0ms is still pretty darn quick, or am I missing something?sevenseacat
Umm Rails is a 3rd party solution :) And adding gems is the rails way as far as I know :)Jan Hančič
@sevenseacat this is only an example, generally compilation for a single file takes around 100msluacassus

3 Answers

2
votes

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.
7
votes

Take a look at this middleware from the Discourse team. We've had great success with it in our rails 4 app -- took reload times down from a minute to 5 seconds in development.

0
votes

As others have pointed out, optimizing your assets is the #1 way to increase compilation speed (eliminate unnecessary files and pre-processors, import carefully, and use partials with caution). However, why are you turning config.assets.debug = false in development mode anyway? When it's false, sprockets will concatenate all your files together, and this can take quite a while if you have a large number of files.

Instead, turn config.assets.debug = true, so assets are compiled and cached on the first request. Sprockets sets a Cache-Control HTTP header to reduce request overhead on subsequent requests. The browser gets a 304 (Not Modified) response. See the Rails Asset Pipeline documentation.