Increase Your Website Performance by Hosting jQuery with a CDN

30. September 2009

In the following screencast I will demonstrate how to speed up your website by using a Content Delivery Network (CDN). Specifically, I will be swamping out a local copy of jQuery with one hosted by Google. You can host a variety of files (example: jQuery, jQuery UI, Prototype, script.aculo.us, MooTools, Dojo, SWFObject, YUI, etc...) with a CDN as well, but the following screencast only shows jQuery being hosted.

First of all, you might be wondering "What is a CDN and why do I need to use one?". If so, you are in luck because Encosia wrote a great blog post concerning this question and here is what he came up with. A CDN provides...

  1. Decreased Latency
  2. Increased Parallelism
  3. Better Caching

For a detailed explanation of the above 3 concept please read his detailed blog post. Now without further ado, here is the screencast.

In the above screencast, I showed how you can replace your local reference of jQuery (see line 4 below) with a version of jQuery that is hosted on a CDN (such as line 5 from Google or line 6 from Microsoft).

<head id="Head1" runat="server">
    <title>Test Page</title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            console.log('jQuery has been loaded & document is ready!');
        });
    </script>
</head>

It is worth to not that even after updating your update to use Google or Microsoft as your CDN, YSlow will still not recognize those files as being hosted on a CDN. YSlow only recognizes itself, Yahoo, as a known CDN. The good news is that you can register other CDNs (such as Google &| Microsoft) deep inside of Firefox's configuration. In order to update this setting you can follow these instructions…

  1. Type in 'about:config' into the Firefox's navigation TextBox
  2. Filter for the 'extensions.yslow.cdnHostnames' Preference Name
  3. Double-Click & enter 'ajax.googleapis.com,ajax.microsoft.com' into the dialog box
  4. Restart Firefox and re-run YSlow
    As I mentioned above, there are many files on CDNs that are available for you to reference in your project. The two CDNs that I highlighted in the above screencast are…

Blog ,


Comments

9/30/2009 2:15:21 PM #
It is nice to check my blog also. Different ways to include JQuery on your web site Smile www.myhtmlworld.com/.../...ry-in-your-website.html
9/30/2009 7:25:08 PM #
The jQuery project is in the middle of setting up a CDN for the project.  This will include CDN hosting of jQuery as well as CDN hosting for any and all jQuery Plugins that exists.  This will be rolled out with the new jQuery plugin repository that is currently being released.  Look for it by the end of the year.  This was announced at jQuery Conference 2009.

I'd personally like to see all CDN hosting for jQuery done with one site.  You start to saturate the CDN hosting of one file it starts to defeat the purpose of the CDN and the file being cached on the client side.
9/30/2009 9:31:31 PM #
Increase Your Website Performance by Hosting jQuery with a CDN

You've been kicked (a good thing) - Trackback from DotNetKicks.com
9/30/2009 10:37:40 PM #
it makes sense, and I know it's true, but I just cannot convince myself to use it.

I don't like the idea of such an INTEGRAL part of my website's functionality being hosted on someone elses server. What if the cdn is down?

and even if it doesn't, what if the user is browsing the web through a proxy/firewall and access to that cdn is restricted? there's no way you would know...

if my site goes down, the whole SITE goes down, but if for whatever reason, my visitors can't access the CDN, then my site is broken, and in my opinion, it's unacceptable to be at the mercy of someone else's bad fortune, however unlikely it may be!
Paul
Paul
10/1/2009 2:06:28 AM #
This article would have been more helpful if it would have implemented a fall-back approach if the CDN is unavailable. This is also useful when developing the website off-line. I have yet to find an elegant way to do this.
10/3/2009 10:15:34 AM #
Great post, I stumbled upon this at work and dug a little deeper into the jQuery cdn's available. But as Josh & Paul point out a big problem with externally hosted content. Even if it is Google / Microsoft to preserve the best user experience you need to handle the case when the script (for whatever reason) doesn't load jQuery.

I posted a block article that addresses just this issue, it may not be elegant but it does work, let me know what you think.

www.aknosis.com/.../
10/3/2009 1:06:50 PM #
Daily tech links for .net and related technologies - October 2-4, 2009

Daily tech links for .net and related technologies - October 2-4, 2009 Web Development Special Folders
10/3/2009 8:19:14 PM #
Thank you for the video tutorial Smile
10/9/2009 5:09:51 PM #
Thanks mate!I was looking for that solution nearly all day!I love your blog,awesome!!!

Regards,
Nick

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading