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?
I created a new gem specifically catered to that requirement. Hope it helps!
https://github.com/kenn/jquery-rails-cdn
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With