jquery-rails-cdn
Add CDN support to
Serving javascripts and stylesheets from a publicly available CDN has clear benefits:
- Speed: Users will be able to download jQuery and others from the closest physical location.
- Caching: CDN is used so widely that potentially your users may not need to download jQuery and others 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.
Features
This gem offers the following features:
- Supports multiple CDN. (Google, Microsoft and jquery.com)
- jQuery and jQuery-UI 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 application.js
are:
- Updating your js code won't evict the entire cache in browsers - your code changes more often than jQuery upgrades, right?
rake assets:precompile
takes less peak memory usage.
Changelog:
- v0.3.3: Allow options.
- v0.3.2: Support using minimized libraries.
- v0.3.1: Fix packaging bug.
- v0.3.0: Microsoft and Yandex are now always scheme-less. (Thanks to @atipugin)
- v0.2.2: Remove Bootstrap and Angular.js to their own rails engine.
- v0.2.1: Add Angular.js
- v0.2.0: Update to match original codes and update bootstrap to 2.1.0
- v0.1.2: Added bootstrap.
- v0.1.1: Added jQuery-UI
- v0.1.0: Added
:google_schemeless
for sites that support both ssl / non-ssl - v0.0.1: Initial release
Installation
Add this line to your application's Gemfile:
gem 'jquery-rails-cdn-yjchen'
Usage
This gem adds these methods to generate a script tag to the jQuery on a CDN of your preference:
jquery_include_tag
and jquery_url
jquery_ui_include_tag
and jquery_ui_url
If you're using asset pipeline with Rails 3.1+, first remove //= require jquery
and //= require jquery-ui
from application.js
.
Then in layout:
= jquery_include_tag :google
= jquery_ui_include_tag :google
= javascript_include_tag 'application'
Note that valid CDN symbols for jQuery and jQuery-UI are:
:google
:microsoft
:jquery
:yandex
Note that valid CDN symbols for bootstrap are:
:default
It will generate the following for jQuery and similarly for jQuery-UI on production:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
window.jQuery || document.write(unescape('%3Cscript src="/assets/jquery-86b29a215ef746103e2469f095a4df9e.js" type="text/javascript">%3C/script>'))
//]]>
</script>
on development:
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
If you want to check the production URL, you can pass :force => true
as an option.
jquery_include_tag :google, :force => true
To fallback to rails assets when CDN is not available, add jquery.js
and jquery-ui.js
in config/environments/production.rb
config.assets.precompile += %w( jquery.js jquery-ui.js )
Options
Set :compressed to use minimized library locally like this:
= jquery_include_tag :default, :compressed => true
Set :local_copy true to use local copy when CDN is not available.
Remember to add assets name in confign/environments/production.rb:
config.assets.precompile += %w( jquery.min.js)
Clarification
jquery-rails-cdn supports jQuery from jquery-rails gem, but not jQuery UI in the same gem. jquery-ui-rails-cdn supports jQuery UI from jquery-ui-rails gem. This one supports both jQuery and jQuery UI from jquery-rails gem.