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, jquery.com, etc.)
- jQuery version is automatically detected via
- 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 unreachable.
On top of that, if you're using asset pipeline, you may have noticed that the major chunks of the code in combined
application.js is jQuery. Implications of externalizing jQuery from
- Updating your JS code won't evict the entire cache in browsers.
- Cached jQuery in the client browsers will survive deployments.
- Your code changes more often than jQuery upgrades, right?
rake assets:precompilewill run faster and use less memory.
- v1.1.0: Support jQuery2. (Thanks to @timurkhafizov)
- v1.0.0: Options like
data-turbolinks-eval: falseare allowed to be passed. (Thanks to @mkitt)
- v0.4.0: Added Cloudflare. (Thanks to @damonmorgan)
- v0.3.0: Microsoft and Yandex are now always scheme-less. (Thanks to @atipugin)
- v0.2.1: Use minified version for Yandex. (Thanks to @atipugin)
- v0.2.0: (Incompatible Change) Google CDN is now always scheme-less. Add Yandex CDN for Russian users. (Thanks to @ai)
- v0.1.0: Added
:google_schemelessfor sites that support both SSL and non-SSL.
- v0.0.1: Initial release
Add this line to your application's Gemfile:
This gem adds two methods
If you're using asset pipeline with Rails 3.1+,
//= require jqueryfrom
- Put the following line in
config/application.rb, so that jquery.js will be served from your server when CDN is not available.
config.assets.precompile += ['jquery.js']
Then in layout:
Note that valid CDN symbols are
Now, 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
If you want to use jQuery 2, drop the following line in
Jquery::Rails::Cdn.major_version = 2
and then in
config.assets.precompile += ['jquery2.js']
To get the same CDN benefits with jQuery UI, we reccomend jquery-ui-rails-cdn.