twitter facebook dribbble email

Using a CDN to serve jQuery

Careful! This post is looking a little old and could be inaccurate in many, many ways

I’ve mentioned before on this blog that I use JQuery as my default JavaScript framework. At one time it just one of many frameworks I would find to suit a specific purpose but because all the cool kids were using it I gave it a proper go and now I don’t think I could live without it.

Following the crowd

As a dedicated follower of fashion the next big thing was to start using a CDN (Content Delivery Network) in order to serve jQuery into your website.

It made sense too. As a meaty 70kb file being able to use someone else’s bandwidth is great, but there were other benefits not least the fact users would cache the file possibly making a visit to your website even quicker as jQuery would not need to be re-downloaded.

But for this to be an advantage you have to use a CDN that the majority of other sites are using, for this Google seemed the obvious choice, but what happens if suddenly the file your using disappears?

Some Code

The answer of course is to have a fallback, self hosted version of jQuery which will be used if your CDN version is no longer available.

But it’s Google

Sure it’s Google but it’s both risky and closed minded to assume nothing could ever go wrong with Google. I’m sure no one expects an XSS attack until it’s too late.

For what it a pretty short bit of code you can ensure your website is never affected by a cavalcade of issues that could befall any file you happen to be using on Google’s CDN. I can tell you I’ve had a couple of times where seemingly the CDN doesn’t appear to be loading, they’re short periods, but it has happened.