Mar 2012

Maximize Your Chances of Caching Your jQuery

by Steve Wortham
Many people will include jQuery on their website using the CDN (content delivery network) from Google, Microsoft, or sometimes jQuery themselves.  There are links for each option on jQuery's site.

This is a good thing for several reasons:

1. Using a CDN in general is a good idea as you're never going to have good performance worldwide with just a single server.  A CDN is optimized to lower the average worldwide latency.
2. Each CDN-hosted copy of jQuery is gzipped.
3. Because so many websites are using these links, there's a good chance that your visitors will have a cached copy of jQuery before ever visiting your site.

There's a catch...
The whole caching issue depends on the links you're using.  I've observed that quite often people aren't linking to exact version numbers of jQuery.

For example, from you can reference this file...

... and you'll always get the latest copy of jQuery.

Or you can do something similar from Google...

That one from Google will get the latest version, assuming it starts with "1". The trouble is that when you use these references you don't get a far-future expiration date set in the cache-control header.  Therefore the user's browser will be checking for updates much too frequently, slowing down your site's load times.

In fact here's a breakdown of several options and their expiration settings...  (no cache)  (1 hour)  (1 hour)  (1 year)

So that last one is what I'd recommend using.  By explicitly stating the exact version of jQuery you want Google sends back a max-age header of 1 year.  So you're greatly increasing the chances that the client will simply use a cached copy.  And if a new version of jQuery comes along with bug fixes or features you really want, then just update your link.