Friday, September 17, 2010

jQuery: host locally or use googleapis.com CDN?

There are two main ways to host your blob of jQuery minimised javascript: on your own webserver or, on Google's. Which is better? A large amount of time has been spent debating that very topic.

What are the pros and cons of using Google?

Pros

  • Fast, geographically distributed, reliable CDN
  • Free, saves using your bandwidth
  • If many people use the Google version of jQuery (and they do), it is highly likely the user will have it in their browser cache, given the long expiry times Google sets when they serve the file (although there are some caveats).  This means the user probably won't have to request the file at all.  Even if users regularly clear their browser cache it is likely to be cached by a proxy.

Cons

  • The CDN might be down, or slow, which will impact your site.
  • If there is no Internet connection it won't work (definitely not the best choice for internal webservers)
  • If you already have other javascript bundled into a minimised file (common practice), it is an extra web request that needs to be made, when you could just include it in the bundle.
  • You are giving Google information about your customers (i.e. forcing them to make a request) to Google.  Given the large amount of caching, this will not be comprehensive, and there is a reasonable chance you are running Google analytics anyway. 
On balance, I think using Google's version is the better option.

No comments: