My Blog

contains PHP and other web related content. (Sometimes there are some off topic things - don't freak out!)

Auto Failover for CDN based Javascript

Using my javascript error reporter code helps me get a better understanding of what my clients are experiencing when visiting my website. One thing I did notice was the failures from time to time of Google’s CDN based Jquery.

To solve this issue, I decided to keep a local copy as well. For the most part, Google’s version is going to be faster and have a better cache method. However, I’d rather have an uncached version of it loaded into the user’s browser than nothing at all! So, in the very rare case that the user’s browser fails to load the Google javascript, I load a local copy of it.

First Step: Load JQuery From Google

The very first thing I do is to include jQuery from google’s code base using the following tag:

1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

This will retrieve the latest version (1.3.2) from Google with the proper compression and cache response I like.

However, this sometimes was failing – well according to my error reporting script. What to do?

Test for a successful load of jQuery

Since the SCRIPT tag does not have an ‘onerror’ standard notifier like our image, the next best thing to do is test for the jQuery object right after the page loads. Since we’re using inline javascript, the first request to the external javascript will block while loading. When it completes, this small snippet will then execute.

I’ll jump ahead one more step though before code.

If no jQuery found, load it dynamically

If the jQuery object is not found, a new script element is created dynamically. The source is pointed to my local version of the script. Finally, it is appended to the head. Hopefully this will load the jquery. If it fails here, well then – the client has some issues!

This is the full inline javascript after the external javascript reference:

?View Code JAVASCRIPT
1
2
3
4
5
6
if (typeof jQuery == 'undefined') {
	var e = document.createElement('script');
	e.src = '/js/noncdn-jquery-1.3.2.js';
	e.type='text/javascript';
	document.getElementsByTagName("head")[0].appendChild(e);
}

In this case, the path http://mydomain.com/js/noncdn-jquery-1.3.2.js contains my local javascript.

The results?

Before this fix, I saw a failed jQuery load about once a day. I have not seen a single one yet. I have yet to see this error reported. If anyone finds a better way to do this, please let me know!

Tags: , ,

4 Responses to “Auto Failover for CDN based Javascript”

  1. Gordon Page says:

    Nice, clever idea.

    When you said: \”Since we’re using inline javascript, the first request to the external javascript will block while loading. When it completes, this small snippet will then execute.\”

    1: Does that mean that other javascript in the same script block will not run until it completes loading, correct?

    2: Does this all happen near instantly when there is a failure on google\’s side, or is there a lengthy timeout?

  2. Aaron says:

    @Gordon Page: 1. This means that the javascript inline block below the external src will wait till the external source loads. You can verify this by inspecting the ‘net’ tab of a tool like firebug. 2. It depends on the failure. If its a DNS failure, then immediately. If its a problem with their CDN serving the content before timeout, well then its the timeout that you have to wait for.

    Hope that helps – thanks for the comment!

  3. Gordon Page says:

    Thanks for the response Aaron, I really appreciate it. I’m going to use this sort of thing for ad serving / cheap failover. Basically serving the swf advertisement from one server, and if that server fails then serve it from another.

  4. [...] 3.http://aaronsaray.com/blog/2009/11/30/auto-failover-for-cdn-based-javascript/ [...]

Leave a Reply

  • twitter loader

Follow me on twitter: @aaronsaray

The views on this website are my own and do not reflect the opinions of my employer or clients.
Creative Commons License Home | Open Source | Book | Music | Art | Bio | Resume | Contact
My Baby