This may be obvious to some, but I've been wondering: why should I depend on Google's server to host jQuery for my site?
Is it only because it loads faster this way?
This may be obvious to some, but I've been wondering: why should I depend on Google's server to host jQuery for my site?
Is it only because it loads faster this way?
This is because:
There are several scenarios when you might not want to use jQuery from Google's CDN:
When you are building an intranet application where the web server is hosted on the same network as the clients. If you use Google's CDN jQuery, you will be making a call to the internet rather than a webserver on the local network. This increases bandwidth for your organization, and is slower.
When you want to run your application offline. (Quite linked to the first issue) If you need to work on a development environment (managed for example with Bower), you might need to be able to make your application work without any internet connection (ie: in a train :)
When you need to customize it. For example if you use Grunt to build the library in order to use only certain modules or setting the AMD name
When you are serving pages over SSL that require jQuery. You should serve the JavaScript over SSL as well as your page to avoid security problems and warnings.
Also, Microsoft hosts jQuery on their CDN. That is another choice comparable to using Google's hosted jQuery.
The greatest benefit is from caching. The theory is that if a visitor visited a site that was loading their JavaScript libraries, say jQuery for example from the Google CDN, then when they visit your website, the library is already in that user’s browser cache and will not have to be downloaded again. This sounds great in theory.
The benefits being shared here and elsewhere are all theoretical. Just came across an in-depth analysis of using a CDN and if it provides the expected performance benefits. http://www.root777.com/appdev/does-using-google-libraries-api-cdn-give-you-performance-benefits
Some good answers here to "Why you should..." and "Why you should not..."
I simply want to add a list of alternatives to Google if you did want to load jQuery from a CDN.
But to sum it up, you are basically improving your overall website/application performance.
Usign CDN with a Service Worker, you can download the CDN once in the client lifetime, and not every time you update your code.