I have to give credit to the authors of the gems I combined and modified to make this a reality: jquery-rails-cdn and jquery-ui-rails-cdn. This gem will utilize jquery-rails however, it will serve your jquery files from the google cdn, if possible, than fall back to your local copies if neccessary.
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 a CDN for jQuery offloads a big one.
This gem offers the following features:
- Supports Google CDN
- jQuery version is set in your application.rb file
- Automatically fallback to local jquery min files stored in your vendor directory:
- 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.
If you're using the asset pipeline with Rails 3.1+,
- Start by removing
//= require jqueryfrom your
If you're using the asset pipeline with Rails 4+,
- Create the jQuery vendor directory:
- Download the version of jquery you want to use from the google cdn and save the file to this directory. The file should look something like
- Verify you have the jquery-rails gem enabled as well as the new gem:
gem 'jquery-rails' gem 'jquery-rails-google-cdn'
- Add these lines to your application.rb file to set the version of jQuery you are using and to make sure the asset pipeline makes it available:
config.assets.precompile += ["jquery.min.js"] config.jquery_version = "2.0.2"
- Finally add this section to your layouts file.
- Please verify you do not have a duplicate
applicationjs include as this can cause issues with libraries down the road like ember. Here is an example layout of a rails 4 application:
The gem will generate the following across all modes production, testing, and development:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script> //<![CDATA[ window.jQuery || document.write(unescape('%3Cscript src="/assets/jquery.min.js">%3C/script>')) //]]> </script>
- v0.0.5: Updated Gem Description
- v0.0.4: Bug Fixes
- v0.0.3: Bug Fixes
- v0.0.2: Changed up initial workflow, forking even further from the previous authors
- v0.0.1: Initial release