Detecting if the JQuery CDN is down and coping with it

4.8k views Asked by At

I'm launching my own site tomorrow, and I'm expecting a couple hundred visits.

I wrote this script based on what PHP.net told me:

<?
$f = fsockopen("code.jquery.com", 80, $errno, $errstr, 30); 
if(!$f){
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>'; }
else {
echo '<script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>'; }
?>

Basically, if code.jquery.com's CDN is ever down (like it was about 20 minutes ago), then the Google API Library will kick in. I've tried it whilst jQuery was up, and it works, but just in case it goes down again, will this script actually work? (by switching over to Google's library) I can't really test it, I'd have to make code.jquery.com go offline. lol.

I'd do CSS replacements, but my entire site is based off of jQuery and Ajax, so I kind of really need it to function at all times. I'd host it on my own site, but my website isn't anywhere as fast as code.jquery.com or googleapis.com when they're running fine.

Thanks alot! Any responses greatly appreciated :)

5

There are 5 answers

3
swatkins On BEST ANSWER

Why would you do this server side? it would make more sense for this to be done client side:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="http://code.jquery.com/jquery-1.6.4.min.js">\x3C/script>')</script>

This first tries to load the library from google. If it loads, then window.jQuery will be available. If not, then it tries to load from code.jquery.com. You can even put in another after those two to load a local version if neither load:

<script>window.jQuery || document.write('<script src="my/js/jquery-1.6.4.min.js">\x3C/script>')</script>
2
Manuel van Rijn On

or you could do this with basic html:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
2
Joe On

It won't do you much good. Putting a fetch into every request will slow things down a huge amount. Also, if you have upstream caching, the change won't be reflected anyway.

I would go for a totally javascript solution: try loading one and if it doesn't work, try the other.

0
Declan Cook On

I believe this would work fine but this adds a lot of overhead to every request made to your server.

Do it client side with JavaScript,

<script src=http://code.jquery.com/jquery-1.6.4.min.js"></script>
 <script>window.jQuery || document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js">\x3C/script>')</script>
0
Adam On

You can test (on a local/test server) by simply putting an entry for code.jquery.com in your hosts file (/etc/hosts on linux, windows\system32\drivers\etc\hosts on windows) pointing at, for example, a local ip which is not used on your network. The system will temporarily see that as DNS for code-jquery.com and will not get a response from there.