Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asset managing with Rails 3 (on Heroku) (Jammit, AssetHat, Rack PageSpeed)

I am interested in the pros and cons of the different tools for managing assets in Rails 3.0.x (especially on Heroku).

There are already some older questions regarding this topic, but in the meanwhile there are some new tools available.

I am especially interested in these tools:

  • Jammit
  • AssetHat
  • Rack PageSpeed

Jammit seems to can do everything that AssetHat can do and is also longer available. So where does AssetHat fit in?

Rack PageSpeed seems to do everything on the fly by directly working on the server response. Did you experience any performance issues by doing that? Would you recommend it over the other two solutions?

like image 530
Zardoz Avatar asked May 27 '11 15:05

Zardoz


2 Answers

Hey there, I'm the author of AssetHat. Minification and concatenation are among the easiest performance boosts to implement; these features are common to Jammit, AssetHat, and rack-pagespeed. Rails has supported concatenation for a long time now (though it's done at runtime, rather than during deployment), and it's no surprise that Rails 3.1 supports both minification and concatenation during deployment.

The remaining features are what make each of these asset managers interesting. For example, Jammit is useful if you want to embed images and font files directly into your stylesheets. rack-pagespeed is also handy if you want to keep all your optimizations in a completely separate layer.

Inlining assets into CSS is great for static pages where stylesheets change infrequently. However, if your site is under active development, and the stylesheet changes even a tiny bit, the user's browser has to re-download the whole thing—including inline images and fonts that probably didn't change. It depends on the nature of your project.

If your assets are too big to inline or concatenate, AssetHat helps optimize for CDNs and parallel loading:

  • It takes great advantage of CDNs, whether it's Google's CDN, cdnjs (which uses Amazon's servers), or another CDN of your choosing. For example, just add <%= include_js :jquery %> to your layout (and a version number in a config file) to load jQuery from Google's CDN. If you're in dev mode and have a local copy of jQuery, that loads instead—easy offline dev.
  • AssetHat can rewrite stylesheets' image URLs to use your CDN instead. This reads from your config.action_controller.asset_host setting, and is done at deploy time. Your original CSS is left untouched.
  • If you have several JS files to load, it's sometimes faster to load them in parallel than to concatenate them (i.e., force them to load serially). You can switch on LABjs mode easily: <%= include_js 'big-file-1', ..., 'big-file-n', :loader => :lab_js %>. If you don't have a copy of LABjs locally, or if you're in production, LABjs loads from Amazon's servers via cdnjs.

By using CDNs like Google's or Amazon's, your users can load more assets in parallel (because there are more hostnames), enjoy greater speed, and sometimes, not even need to download assets at all (e.g., if they already loaded Google's copy of jQuery via someone else's website).

I've used AssetHat on Heroku by setting my deploy script to simply run rake asset_hat:minify (to minify and concatenate CSS/JS), commit those changes to my repository, then do the actual deployment.

In case you haven't seen these already, you might be interested in:

  • a longer walkthrough of AssetHat's features
  • the official website
  • the technical readme
  • the extensive docs

If you need help setting it up, or have any other questions, feel free to message me on GitHub (rondevera) or Twitter (@ronalddevera).

like image 142
Ron DeVera Avatar answered Nov 14 '22 22:11

Ron DeVera


Jammit won't work out of the box on Heroku as far as I know. One option seems to be to use the Heroku Jammit plugin to manage your assets - https://github.com/chebyte/heroku-jammit.

Alternatively, Jammit can be configured to output to /tmp: http://geekninja.blogspot.com/2011/04/making-jammit-jam-with-heroku.html

Rails 3.1 will include Sprockets to handle asset packaging, I think that's worth considering.

like image 25
Alex Korban Avatar answered Nov 15 '22 00:11

Alex Korban