Coder Social home page Coder Social logo

jquery-rails-cdn's Introduction

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.

jquery-rails-cdn's People

Contributors

ai avatar atipugin avatar confusedvorlon avatar damonmorgan avatar felixbuenemann avatar hlascelles avatar jason-cooke avatar kenn avatar mkitt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-rails-cdn's Issues

jquery still need to be compiled separately

Since jquery asset is the fall-back solution when cdn is not available, it should be mentioned that jquery.js still need to be precompiled in production . In this case, it should be added into config.assets.precompile of config/environments/production.

jquery-ui

Hi, awesome gem! What are your thoughts on including support for jquery-ui?

Jquery.js is correct, but google cdn version is still 1.8.3

I'm not sure if this is a problem on my end (or something to do with jquery-rails), but in my app, the code jquery-rails-cdn generates seems to be off by a version. For the ajax.googleapis.com URL it says 1.8.3, but for local precompiled version (jquery.js) its the correct version (1.9.0)

Thanks!

Major version not honored for local jQuery versions

The local version of jQuery is currently hardcoded to v1 due to using :jquery as symbol. Setting Jquery::Rails::Cdn.major_version = 2 doesn't change the behaviour and leads to between the version loaded from CDN in production mode and the jQuery version used during development, test or while being offline.

Rails 3 (3.2.12) production: uninitialized constant Jquery::Rails::JQUERY_2_VERSION

Hello, (first issue I do- be gentle)
In version 1.1.0 you added the use of constant Jquery::Rails::JQUERY_2_VERSION in lib/jquery-rails-cdn.rb

But I believe it's only added by rails4 in the jquery-rails gem- http://www.rubydoc.info/gems/jquery-rails/4.0.5/Jquery/Rails
I use jquery-rails-3.1.4 (I still use rails 3), which doesn't have that constant defined- and since jquery-rails-4.0.5 (or higher) isn't a dependency of jquery-rails-cdn- It just doesn't have it- and eventually the production environment fail bcz this constant isn't defined.

My solution was to use jquery-rails-cdn version 1.0.3 where the Jquery::Rails::JQUERY_2_VERSION isn't used there. (it was added in jquery-rails-cdn version 1.1.0)

Hope I made myself clear.
Have a nice day.

Way to get new jQuery before jquery-rails

jQuery team release 1.8.0 version, but in jquery-rails is still 1.7.2. They are too slow. I can put new version in vendor/assets/javascripts/, but jquery-rails-cdn rake jQuery version only from jquery-rails.

So, maybe we need to add option, like:

= jquery_include_tag :google, version: '1.8.0'

If you agree, I can create pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.