How does a rails application include javascript?

I am very curious how your Rails applications include javascript. For example:

  • Do you pack all your js code into one file and serve it for all requests?
  • Are you conditionally loading certain js depending on the controller / action?
  • what tools or methods do you use, for example: asset_packager, yui compressor, stars, implementation inspired by BigPipe?

A bit of background: I'm working on a massive Rails application that is very heavy on JS. Currently, all js are minimized and served from a single file. This makes things very convenient, since all frameworks and widgets are available everywhere. I'm starting to doubt this approach, it seems a little crazy to make all users pay for some js that they never see. Mashing code with a script involves crappy and complex ones, as large parts of the site deliver content via ajax.

Does anyone have any tips to share?

Thank you so much!

+6
javascript jquery prototypejs ajax ruby-on-rails
source share
2 answers

Several tradeoffs can be considered:

  • If you have one large JS file, it should be cached for all of your pages. But if only a few pages use JS, then this is not good.
  • If your pages do not transmit JS, you can use the β€œas needed” loading for each individual JS page. But you do not want too many loads, since each choice of JS has its own overhead.

Notes

  • Make sure all your JSs are cached forever on your clients. Use version numbers in file names or URLs (foo.js? 123)
  • Make sure the JS files are minimized.
  • Make sure gzip encoding is enabled on the web server.
  • You can use a low-cost content delivery network for your JS, such as Amazon Cloudfront or one of your competitors.

Answers to your specific questions

Do you pack all your js code into one file and serve it for all requests?

All requests receive a large JS file, which has library code and JS used on most pages. Some specific pages also receive an additional JS file.

Are you conditionally loading certain js depending on the controller / action?

Yes, for a couple of very heavy JS pages, these pages get an additional JS file. All pages receive a registry of JS files and are cached / available for all pages.

what tools or methods do you use, for example: asset_packager, yui compressor, stars, implementation inspired by BigPipe?

  • YUI compressor
  • S3 to service my asset.foo.com domain
  • rake tasks to combine / minimize multiple JS source files
+3
source share

First of all, try this plugin for Firefox: http://developer.yahoo.com/yslow/ . This will help you identify sources of slowness and analyze script usage and suggest improvements.

Also, check out this Yahoo article about best practices for fast sites: http://developer.yahoo.com/performance/rules.html . This tip applies to all websites, not just rails.

+1
source share

All Articles