• This repository has been archived on 10/May/2021
  • Stars
    star
    59
  • Rank 509,601 (Top 11 %)
  • Language
    Ruby
  • License
    Other
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Best way to use latest jQuery in Ruby app

DEPRECATED: Best way to load latest jQuery in Ruby

This project is depreacted. Use Node.js build tools instead of Sprockets.

Load jQuery from CDN in production and use local copy in development. jQuery-CDN supports Ruby on Rails, Sinatra and other non-Rails environments with Sprockets.

Another gem jquery-rails contains also UJS adapter for jQuery. So it need to test any jQuery updates and will release new jQuery version after few month (for example, there is still no jQuery 2 in jquery-ujs). If you don’t need UJS, this gem will be better for you.

jQuery-CDN now has 2 branches: with jQuery 2.x and 1.x.

Features

Load from CDN

Public CDN is a best way to serve jQuery:

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

In development gem will use local copy of jQuery, so you can develop app in airplane without Internet. In production gem will use CDN, but if it will down, gem will automatically fallback to bundled jQuery.

Latest version of jQuery

Instead of jquery-rails this gem always contain latest version of jQuery, because it doesn’t need to test compatibility with UJS adapter.

For example, jquery-rails doesn’t support jQuery 2 even after 4 months.

Gem version is same that jQuery

Instead of jquery-rails, this gem versions tell exactly what jQuery is inside. Gemfile maintaining will be much easy:

gem 'jquery-cdn', '1.10.2' # Use jQuery 1.10.2

Sinatra and plain Ruby support

You can use jQuery-CDN with Ruby on Rails, Sinatra or any other Ruby environment with Sprockets.

How To

Ruby on Rails

Add jquery-cdn gem to your Gemfile:

gem 'jquery-cdn'

If you support IE 6, 7 or 8, lock jQuery in 1.x versions:

gem 'jquery-cdn', '~> 1.0'

Call include_jquery helper in layout:

!!! 5
%html
  %head
    %title My site
    = include_jquery
    = javascript_include_tag('application')

Ruby

If you use Sinatra or other non-Rails frameworks with Sprockets, just connect your Sprockets environment to jQuery-CDN:

require 'jquery-cdn'

assets = Sprockets::Environment.new do |env|
  # Your assets settings
end

JqueryCdn.install(assets)

Set local jQuery URL (by default, /assets/jquery.js):

JqueryCdn.local_url = proc { '/jquery.js' }

Include JqueryCdn::Helpers module to your app:

class YourApp < Sinatra::Base
  helpers { include JqueryCdn::Helpers }
end

And use include_jquery helper with env option:

!!! 5
%html
  %head
    = include_jquery(env: app.environment)

Options

Helper include_jquery has 2 options:

  • env: CDN will be used only in :production environment. Rails helper can detect it automatically. By default, :production.
  • cdn: CDN to use. By default, :google.

Other options will be used as <script> attributes.

CDNs

By default, gem use Google CDN, but you can change it by cdn option:

= include_jquery cdn: :yandex

You can use :google, :microsoft, :jquery, :yandex or :cloudflare CDN.

Defer

Scripts with defer attribute will be executed only after <body> loading. They increase perfomance, because scripts without defer block DOM parsing, until script is downloading.

This attribute works like you put <script> tags to end of <body>. But defer is better, because scripts downloading will start early.

So, defer attributes is highly recommended, but you need add it to all your scripts.

!!! 5
%html
  %head
    %title My site
    = include_jquery(defer: true)
    = javascript_include_tag('application', defer: true)

Note, that unfortunately jQuery-CDN can’t use fallback with defer now, because this options can’t work with inline scripts.

Fallback

According to Murphy’s Law, even Google CDN may go down. So when you write include_jquery, jQuery CDN inserts:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>window.jQuery || document.write(unescape('%3Cscript src="/assets/jquery.js">%3C/script>'))</script>

This HTML checks, is jQuery normally loaded from Google. On any problems it will load local copy of jQuery.

More Repositories

1

nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
JavaScript
24,064
star
2

easings.net

Easing Functions Cheat Sheet
CSS
7,908
star
3

size-limit

Calculate the real cost to run your JS app or lib to keep good performance. Show error in pull request if the cost exceeds the limit.
JavaScript
6,487
star
4

visibilityjs

Wrapper for the Page Visibility API
JavaScript
1,825
star
5

nanoevents

Simple and tiny (107 bytes) event emitter library for JavaScript
TypeScript
1,442
star
6

autoprefixer-rails

Autoprefixer for Ruby and Ruby on Rails
Ruby
1,213
star
7

nanocolors

Use picocolors instead. It is 3 times smaller and 50% faster.
JavaScript
868
star
8

audio-recorder-polyfill

MediaRecorder polyfill to record audio in Edge and Safari
JavaScript
580
star
9

keyux

JS library to improve keyboard UI of web apps
TypeScript
380
star
10

webp-in-css

PostCSS plugin and tiny JS script (131 bytes) to use WebP in CSS background
JavaScript
346
star
11

offscreen-canvas

Polyfill for OffscreenCanvas to move Three.js/WebGL/2D canvas to Web Worker
JavaScript
332
star
12

convert-layout

JS library to convert text from one keyboard layout to other
JavaScript
251
star
13

ssdeploy

Netlify replacement to deploy simple websites with better flexibility, speed and without vendor lock-in
JavaScript
194
star
14

environment

My home config, scripts and installation process
Shell
193
star
15

nanodelay

A tiny (37 bytes) Promise wrapper around setTimeout
JavaScript
189
star
16

dual-publish

Publish JS project as dual ES modules and CommonJS package to npm
JavaScript
186
star
17

nanospy

Spy and mock methods in tests with great TypeScript support
TypeScript
138
star
18

check-dts

Unit tests for TypeScript definitions in your JS open source library
JavaScript
138
star
19

autoprefixer-core

autoprefixer-core was depreacted, use autoprefixer
JavaScript
136
star
20

transition-events

jQuery plugin to set listeners to CSS Transition animation end or specific part
JavaScript
133
star
21

evil-blocks

Tiny framework for web pages to split your app to separated blocks
JavaScript
127
star
22

rails-sass-images

Sass functions and mixins to inline images and get images size
Ruby
114
star
23

compass.js

Compass.js allow you to get compass heading in JavaScript by PhoneGap, iOS API or GPS hack.
CoffeeScript
112
star
24

evil-front

Helpers for frontend from Evil Martians
Ruby
101
star
25

rake-completion

Bash completion support for Rake
Shell
63
star
26

yaspeller-ci

Fast spelling check for Travis CI
JavaScript
61
star
27

sitnik.ru

My homepage content and scripts
JavaScript
57
star
28

pages.js

CoffeeScript
44
star
29

fotoramajs

Fotorama for Ruby on Rails
Ruby
44
star
30

about-postcss

Keynotes about PostCSS
Ruby
29
star
31

autohide-battery

GNOME Shell extension to hide battery icon in top panel, if battery is fully charged and AC is connected.
JavaScript
28
star
32

darian

Darian Mars calendar converter
Ruby
25
star
33

better-node-test

The CLI shortcut for node --test runner with TypeScript
JavaScript
25
star
34

plain_record

Data persistence with human readable and editable storage.
Ruby
24
star
35

evolu-lang

Programming language to automatically generate programs by evolution (genetic programming).
JavaScript
22
star
36

martian-logux-demo

TypeScript
17
star
37

hide-keyboard-layout

GNOME Shell extension to hide keyboard layout indicator in status bar
JavaScript
17
star
38

twitter2vk

Script to automatically repost statuses from Twitter to VK (В Контакте)
Ruby
16
star
39

asdf-cache-action

A Github Action to install runtimes by asdf CLI with a cache
15
star
40

ci-job-number

Return CI job number to run huge tests only on first job
JavaScript
15
star
41

print-snapshots

Print Jest snapshots to check CLI output of your tool
JavaScript
15
star
42

load-resources

Load all JS/CSS files from site website
JavaScript
15
star
43

susedko

Fedora CoreOS ignition config for my home server
JavaScript
14
star
44

file-container

Store different languages in one source file
JavaScript
14
star
45

postcss-isolation

Fix global CSS with PostCSS
14
star
46

autoprefixer-cli

CLI for Autoprefixer
JavaScript
14
star
47

d2na

D²NA language for genetic programming
Ruby
11
star
48

showbox

Keynote generator
JavaScript
11
star
49

boilerplates

Boilerplate for my open source projects
JavaScript
9
star
50

postcss-way

Keynotes about PostCSS way
9
star
51

gulp-bench-summary

Display gulp-bench results in nice table view
JavaScript
8
star
52

universal-layout

Универсальная раскладка Ситника
8
star
53

anim2012

Доклад «Анимации по-новому — лень, гордыня и нетерпимость»
CSS
8
star
54

nanopurify

A tiny (from 337 bytes) HTML sanitizer
JavaScript
7
star
55

ai

6
star
56

rit3d

Доклад «Веб, теперь в 3D: Практика»
CSS
6
star
57

dis.spbstu.ru

Department homepage
Ruby
5
star
58

jstransformer-lowlight

Lowlight support for JSTransformers
JavaScript
5
star
59

jest-ci

CLI for Jest test framework, but coverage only on first CI job
JavaScript
5
star
60

evolu-steam

Evolu Steam – evolutionary computation for JavaScript
JavaScript
5
star
61

insomnis

Текст блогокниги «Инсомнис»
4
star
62

plague

Blog/book Plague engine
Ruby
4
star
63

wsd2013

Презентация «Автопрефиксер: мир без CSS-префиксов»
Ruby
4
star
64

showbox-bright

Shower Bright theme for Showbox
JavaScript
3
star
65

ruby2jar

Ruby2Jar builds JAR from a Ruby script
Ruby
3
star
66

showbox-ai

Sitnik’s theme for ShowBox
CSS
3
star
67

showbox-shower

Shower for ShowBox
JavaScript
2
star
68

on_the_islands

Ruby
2
star