Skip to content
프로젝트

kenn/jquery-rails-cdn

GitHub에서 보기

소개

Add CDN support to jquery-rails

기타 프로젝트

이 수치를 측정하는 방법

What you can do

  • Supports multiple CDN. (Google, Microsoft, jquery.com, etc.)
  • jQuery version is automatically detected via jquery-rails.
  • You're on a development environment, so that you can work offline.
  • The CDN is down or unreachable.

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

jquery-rails-cdn

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.

Features

This gem offers the following features:

  • Supports multiple CDN. (Google, Microsoft, jquery.com, etc.)
  • 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 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 application.js are:

  • 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:precompile will run faster and use less memory.

Changelog:

  • v1.2.0: Support jQuery3. (Thanks to @CUnknown)
  • v1.1.0: Support jQuery2. (Thanks to @timurkhafizov)
  • v1.0.0: Options like defer: true or data-turbolinks-eval: false are 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_schemeless for sites that support both SSL and non-SSL.
  • v0.0.1: Initial release

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+,

  • Remove //= require jquery from application.js.
  • 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:

= 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.

관련 프로젝트 살펴보기

불러오는 중…