Kind of self-explanatory question: Why is it recommended loading jQuery or any other library from a CDN?
4 Answers
- A CDN will be quicker than your server.
- Servers have maximum number of connections. Browsers restrict the number of simultaneous requests to a single domain. Off loading your libraries to CDN stops both of these.
- If everyone loads their jQuery library from Google's CDN, the resource will be shared between websites, and the same version of jQuery doesn't need to be loaded multiple times (from different servers).
- It's cheaper for you ;) - Save bandwidth!
-
Generally agree but it's worth remembering that it may not always be quicker - the overhead of DNS lookup and creating the initial connection may be greater than serving it from your own server. Also recent research shows the likelihood of having the version of jQuery present in cache from another site is pretty low. Dec 14, 2011 at 19:34
-
@AndyDavies: Fair point on the DNS lookup, but I'm not sure what you mean by the initial connection time? Have you got a link to that research; I remember reading it, but cannot find the link anymore (something in my head tells me it was done by Facebook?)– MattDec 16, 2011 at 9:45
-
Here's the article - statichtml.com/2011/google-ajax-libraries-caching.html - it was by Steve Webster. By initial connection time I mean the time to set up the TCP connection to the Google CDN - I've got an example that I'll stick an example image up. Dec 16, 2011 at 9:56
Two main reasons as far as i know:
1 - It can speed delivery of the hosted files by providing a more proximate route to the client.
2 - It can provide caching benefits for commonly used files since the user may have already downloaded the file from the same cdn
@Matt hit the nail on the head. In addition to a CDN, you can do a fallback to your local version of jQuery in case the CDN goes down. Example below:
<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> -->
<script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>
There are many reasons, but here are a few that stand out:
- Saves you bandwidth. For something that is not custom, why waste your site's bandwidth when you can rely on google, MS or yahoo to host it.
- If a lot of sites use a hosted version, then the probability that the user's browser will have it cached already increases. This means one less download to visit your site.
- The CDN may have a location more geographically appropraite for you users and be able to serve it up much faster.
- Since the hosted version will be in a different domain, the bowser limitation of simultaneous downloads will be avoided.
Many people say that it is risky to rely on the CDN, as what happens if the CDN goes down. This is true, but most likely your site will be more likely to go down that the big guys. That being said, there are approaches that can be used to have a fallback to a version hosted on your site if the CDN does go down.