Quick Google CDN jQuery Tip
Here's a quick tip if your using Google's Content Delivery Network (CDN) for serving up jQuery (or other scripts).
So if you want jQuery 1.4.4 you can use a url like this:
Now suppose you just want the latest 1.4.x release, you can use (caution, keep reading):
And if you just want the latest 1.x.x release, you can use this (caution, keep reading):
I would think that using a less specific version number (like 1.4 or just 1) would result in a higher cache hit ratio for your visitors. One drawback to this would be that when a new version comes out your users will start using it without giving you a chance to do any testing.
Update: However if you take a look at the comments, one of my readers points out that there is a big difference in the
Expires http header when using the full version, vs using a version shortcut. Here's what the HTTP headers look like on a request for
Date: Thu, 13 Jan 2011 16:41:02 GMT Expires: Fri, 13 Jan 2012 16:41:02 GMT Cache-Control: public, max-age=31536000
Now here's what they look like when you request just the
Date: Fri, 14 Jan 2011 16:45:21 GMT Expires: Fri, 14 Jan 2011 15:45:21 GMT Cache-Control: public, must-revalidate, proxy-revalidate, max-age=3600
1.4.4 we get a file with an expiration date of next year, and the
Cache-Control with a
max-age of one year.
Now when you look at the request for
1.4 you will notice that the
Expires date is prior to the
Date header, and the
max-age specified in the
Cache-Control header is only one hour.
What are the Pro's and Con's to using Google's CDN to serve up jQuery?
- Google's CDN has a very low latency, it can serve a resource faster than your webserver can.
- There is a good chance that the user may have a cached copy of jQuery from google CDN, so they won't need to download it again.
- Chance are good that the dns lookup for
ajax.googleapis.comis cached by the user as well.
- Because the script is on a separate domain, modern browsers will download the script in parallel with scripts on your domain.
- Google's CDN also works with HTTPS, so you can offload the SSL processing to google's server.
- Additional DNS request may be required, but I'm betting the Pro's make this negligible in most cases.
- May be slower if your site is internal (an intranet for example).
Like this? Follow me ↯Tweet Follow @pfreitag
Quick Google CDN jQuery Tip was first published on January 13, 2011.
If you like reading about google, cdn, jquery, tips, or ajax then you might also like:
- jQuery UI Sortable Tutorial
- GoogleBot Runs AJAX?
- 4 Google Tricks
- Search Engine Ranking Factors
- AdSense Optimization Tips (from google)
- Google Calculator