Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CDN hosted JS libraries for production in Rails 3.1

I'm experimenting with Rails 3.1 which uses Sprokets 2.0 as its "asset pipeline" and trying to set it up to use CDN-hosted libraries when in production, but local hosted file for development.

I.e., in development: jquery.js is loaded as part of application.js

but in production: http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js is loaded instead.

Bonus question: any reason why I should not do this?

like image 611
chinclow Avatar asked Aug 06 '11 04:08

chinclow


2 Answers

I created a new gem specifically catered to that requirement. Hope it helps!

https://github.com/kenn/jquery-rails-cdn

like image 144
kenn Avatar answered Nov 15 '22 10:11

kenn


There is no reason to not do this. The point of Google's CDN is that when many different sites us the same CDN file the chances are it will be already cached by the browser or somewhere else. The whole point of the pipeline is to do the same thing - compress + package + far-future headers.

The only reason to not use it is that the CDN is far away and few sites that you visitors use also use the CDN.

In my case I am in NZ, and few local sites of any note are using the CDN. This adds a high first request penalty to people visiting our site (no-one has got the file while browsing another local site). I've opted to roll jquery into the production bundle to avoid the requests and the time spent loading (about 700mS from NZ).

YMMV!

like image 28
Richard Hulse Avatar answered Nov 15 '22 09:11

Richard Hulse