Home

Awesome

jquery-rails-cdn

Add CDN support to jquery-rails.

Serving jQuery from a publicly available CDN has clear benefits:

Features

This gem offers the following features:

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 application.js are:

Changelog:

Installation

Add this line to your application's Gemfile:

gem 'jquery-rails-cdn'

Usage

This gem adds two methods jquery_include_tag and jquery_url.

If you're using asset pipeline with Rails 3.1+,

config.assets.precompile += ['jquery.js']

Then in layout:

= jquery_include_tag :google
= javascript_include_tag 'application'

Note that valid CDN symbols are :google, :microsoft, :jquery, :cloudflare and :yandex.

Now, it will generate the following on production:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
window.jQuery || document.write(unescape('%3Cscript src="/assets/jquery-3aaa3fa0b0207a1abcd30555987cd4cc.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

jQuery 2 / jQuery 3

If you want to use jQuery 2, drop the following line in config/initializers/jquery_cdn.rb:

Jquery::Rails::Cdn.major_version = 2

or for jQuery 3:

Jquery::Rails::Cdn.major_version = 3

and then in config/application.rb:

config.assets.precompile += ['jquery2.js']

or for jQuery 3:

config.assets.precompile += ['jquery3.js']

jQuery UI

To get the same CDN benefits with jQuery UI, we recommend jquery-ui-rails-cdn.