The main advantage of using CDN (content delivery network) is that, given their widespread use, it is likely that your visitor may already have a cached copy of the script that you are trying to download in your browser. This completely negates the load time. If they donβt have a cached copy, the likelihood that the CDN will deliver them to them faster than your server can in any case. In my opinion, it is best to use CDN where possible.
Even with this in mind, CDNs are not infallible, and you do not want your site to rely 100% on another server. I would advise getting a local copy of your scripts and using them as a backup where possible. For jQuery, this is pretty simple:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript"> if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='/Scripts/jquery-1.7.1.min.js' type='text/javascript'%3E%3C/script%3E")); } </script>
Other libraries may differ in their testing methods if they are loaded, but the idea is the same.
It is also worth noting that if you download ALWAYS from Google CDN, use the full version number, otherwise the script will not be cached.
That is, if your request URL looks like this:
"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" // highest 1.4 version (1.4.4) "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" // latest version (1.7.1)
The expires header is set to the current date, so the caching effect is invalidated.
Read more about it here.
source share