Add CDN support to jquery-rails.
Serving jQuery from a publicly available CDN has clear benefits:
- Speed: Users will be able to download jQuery from the closest physical location.
- Caching: CDN is used so widely that potentially your users may not need to download jQuery at all.
- Parallelism: Browsers have a limitation on how many connections can be made to a single host. Using CDN for jQuery offloads a big one.
This gem offers the following features:
- Supports multiple CDN. (Google, Microsoft and jquery.com)
- jQuery version is automatically detected via jquery-rails.
- Automatically fallback to jquery-rails' bundled jQuery when:
- You're on a development environment, so that you can work offline.
- The CDN is down or unavailable.
On top of that, if you're using asset pipeline, you may have noticed that the major chunks of the code in
application.js is jQuery. Implications of externalizing jQuery from
- Updating your js code won't evict the entire cache in browsers - your code changes more often than jQuery upgrades, right?
rake assets:precompiletakes less peak memory usage.
Add this line to your application's Gemfile:
This gem adds two methods
jquery_url to generate a script tag to the jQuery on a CDN of your preference.
If you're using asset pipeline with Rails 3.1+, first remove
//= require jquery from
Then in layout:
Note that valid CDN symbols are
It will generate the following on production:
If you want to check the production URL, you can pass
:force => true as an option.
jquery_include_tag :google, :force => true