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
Larry k
source share