Archive for May, 2008

18 May 2008

How to create a YUI Compressor MSBuild Task

18 Comments Uncategorized

Recently for IdeaPipe I have been looking for ways to deliver my content more quickly and reduce unnecessary bandwidth use.

According to Yahoo’s Performance Team more than half of the viewers of the Yahoo websites start with an empty cache, which means the browser has to download all the resources for the first time. This combined with a high traffic website and unneeded white space and comments can really add up to a significant bandwidth use. There are many popular ways to minify your static content tax on your bandwidth, using many popular tools, as described in this excerpt from Yahoo:

In terms of code minification, the most widely used tools to minify JavaScript code are Douglas Crockford’s JSMIN, the Dojo compressor and Dean Edwards’ Packer. Each of these tools, however, has drawbacks. JSMIN, for example, does not yield optimal savings (due to its simple algorithm, it must leave many line feed characters in the code in order not to introduce any new bugs).

The goal of JavaScript and CSS minification is always to preserve the operational qualities of the code while reducing its overall byte footprint (both in raw terms and after gzipping, as most JavaScript and CSS served from production web servers is gzipped as part of the HTTP protocol).

The cream of the crop seems to be a tool Yahoo developed to deliver its own static text content scripts and styles, the YUI Compressor:

The YUI Compressor is JavaScript minifier designed to be 100% safe and yield a higher compression ratio than most other tools. Tests on the YUI Library have shown savings of over 20% compared to JSMin (becoming 10% after HTTP compression). Starting with version 2.0, the YUI Compressor is also able to compress CSS files by using a port of Isaac Schlueter‘s regular-expression-based CSS minifier.

The YUI Compressor is a Java JAR file that can be download from Julien Lecomte Blog.

The YUI Compressor yielded exceptional results, however it was missing one thing. Integration in to my build and deployment process. In IdeaPipe I use a MSBuild script to compile, manipulate, and prepare for publishing. So naturally I built a MSBuild Task to minimize my JavaScript and CSS files.

The magic actually happens by invoking Java in an external process for each file passed in to the task.

Process process = new Process();
process.StartInfo = new ProcessStartInfo {
	FileName = @"c:\program files\java\jdk1.6.0_06\bin\java.exe",
	Arguments = String.Format(
		@"-jar ""C:\development\tools\yuicompressor-2.3.5.jar"" --type {0} --charset utf8 {1} -o ""{2}"" ""{3}""",
		type,
		ShowWarnings ? "--verbose" : String.Empty,
		newFile,
		oldFile
		),
	UseShellExecute = false,
	CreateNoWindow = true,
	RedirectStandardOutput = true,
	RedirectStandardError = true
};
process.Start();
process.WaitForExit(5000);

Then I read the warning from the standard error output and send them back to Visual Studio as a compile warning if the ShowWarning property is true.

string[] warnings = process.StandardError.ReadToEnd()
	.Replace("\r", String.Empty)
	.Split(new string[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);

foreach(string warning in warnings)
	Log.LogWarning(null, null, null, oldFile, 1, 1, 1, 1, FormatWarning(warning), null);

To integrate this in to my MSBuild script I had to first register my task:

<UsingTask TaskName="ManagedFusion.Build.YuiCompress" AssemblyFile="$(ProjectDir)..\ManagedFusion.Build\bin\$(ConfigurationName)\ManagedFusion.Build.dll"/>

Then setup my ItemGroup for the files:

<ItemGroup>
	<JavaScriptContent Include="$(SourceWebPhysicalPath)\**\*.js" />
	<CssContent Include="$(SourceWebPhysicalPath)\**\*.css" />
</ItemGroup>

Then finally I setup my task to perform the minimization against the JavaScript and CSS files seperately:

<Target Name="AfterBuild">
	<!-- do other stuff to prepare for publishing -->
	<YuiCompress Files="@(JavaScriptContent)" Type="JS" />
	<YuiCompress Files="@(CssContent)" Type="CSS" />
</Target>

You can easily incorporate this in to your own MSBuild scripts or even your Visual Studio Project which is just an MSBuild file for compiling your source code for the project. I have included my source code below:

Download: YUI Compressor MSBuild Task Source

Note: There are a couple of static paths to be on the look out for and modify as necessary for your own code. In my code the Java runtime is loaded at c:\program files\java\jdk1.6.0_06\bin\java.exe and the YUI JAR is located at C:\development\tools\yuicompressor-2.3.5.jar.

Update (2008-5-21): Thanks George, apparently IIS doesn’t like serving straight C# files. So I added the code to my Coder Journal Source Control, so that it can be downloaded from there.

15 May 2008

What I learned about SEO from Celebrity Jeopardy!

5 Comments Uncategorized

I was having a conversation with my best friend a few days ago and we got on the subject of our preferences for how URL’s are rendered for blogs.

I fall on the side of lowercase letters and hyphens splitting the words:

http://www.somesite.com/2008/05/my-url-preference-is-like-this/

He falls on the side of title case lettering and no hyphens splitting the words:

http://www.somesite.com/2008/05/HisURLPreferenceIsLikeThis/

He has his reasons I have mine, I just think mine are more valid. Sorry Al that is my opinion. I am going to layout why I think mine are more valid, with an example from Celebrity Jeopardy. For those of you who aren’t familiar with this famous skit:

Celebrity Jeopardy! was a recurring sketch on Saturday Night Live. It parodies the Celebrity Jeopardy! edition of the television game show Jeopardy! where celebrities compete and the game’s level of difficulty is significantly reduced. Thirteen sketches have been aired to date, two per season from 1996 to 2002, and one in 2005.

Before I get to my commentary lets first watch this excerpt from Saturday Night Live’s Celebrity Jeopardy!:

I really tried to find a good example of one of the famous Sean Connery mess ups in a legal video sharing site, but none of them had anything usable. The skit I was really looking for was the famous “An Album Cover” where Sean Connery pronounces it as “Anal Bum Cover”.

As you can see Norm MacDonald playing the character of Burt Reynolds transforms the category in Celebrity Jeopordy!, on purpose for comedy reasons. In my analogy Google is going to be the Burt Reynolds of your search, however instead of finding the wrong words on purpose it is going to do it because it is a dumb machine that does what it is asked even if the results are not contextually accurate.

An Album Cover Google Example

Notice in the image above, in the highlighted words, Google finds both “An Album Cover” and “Anal Bum Cover”. This is because Google understands that the words you may be looking for don’t always fall in the same order and spacing as the exact phase you are looking for. This is something that SEO experts have known for a long and try to control so that their content shows in the top spot for the keywords they designed in to the page.

If you don’t control your URL, which is one of the highest ranking keywords on your site. You could end up decreasing the effectiveness of your keywords, as an almost duplicate keyword penalty. Granted I don’t know if something like this exists as a penalty, but when you are dealing with SEO it never hurts to be as careful and precise as possible.

So again I ask which URL would you rather have? Now knowing how a URL can be misconstrued :

http://www.somesite.com/2008/05/an-album-cover/
http://www.somesite.com/2008/05/AnAlbumCover/

So this is my way of saying be careful what your URL spells out, you may get unintended search rankings that you may not want, or you may offend a person who reads the URL wrong. Either way it is always good to control your environment with in reasonable means to make sure the message is received as you were intending it.

note: There are other factors in play that yielded the search results above.  However one thing that you will notice is that none of the URL’s were falsely highlighted, that is because they used a non-whitespace character to break up the words.

13 May 2008

2 Easy Steps To Turn Your Blog Into An OpenID Gateway

4 Comments Uncategorized

Many of you probably have heard of OpenID, but have never had a chance to use it. However, I predict that most of you reading this blog will have used it by the end of the year. I can make this prediction with an almost 100% certainty because there is a growing movement behind it that has many big players actively buying developing and integrating their platforms with the OpenID protocol. Some of the biggest players are:

  1. AOL
  2. Yahoo
  3. Google
  4. Microsoft

OpenID according to the official OpenID site is explained as the following:

OpenID eliminates the need for multiple usernames across different websites, simplifying your online experience.

You get to choose the OpenID Provider that best meets your needs and most importantly that you trust. At the same time, your OpenID can stay with you, no matter which Provider you move to. And best of all, the OpenID technology is not proprietary and is completely free.

To facilitate my prediction, of most of you using OpenID by the end of the year, I am going to give you 2 easy steps to turn your blog, or any website, in to a OpenID gateway. That will work for OpenID 1.0, 1.1, and 2.0 versions of the protocol.

The first thing you need to turn your blog into an OpenID Gateway is an account with an OpenID provider, my favorite is MyOpenID, because of the abundance of features offered, but more providers can be found on http://openid.net/get/.

The second thing is pretty easy to accomplish and just involves adding some meta data to your HTML header. Just take the following snippet and replace {youraccount.myopenid.com} with your OpenID provider URL that was provided to you (there are 3 places in the snippet to replace the URL):

  <link rel="openid.server"
        href="http://www.myopenid.com/server" />
  <link rel="openid.delegate"
        href="http://{youraccount.myopenid.com}/" />
  <link rel="openid2.local_id"
        href="http://{youraccount.myopenid.com}" />
  <link rel="openid2.provider"
        href="http://www.myopenid.com/server" />
  <meta http-equiv="X-XRDS-Location"
        content="http://www.myopenid.com/xrds?username={youraccount.myopenid.com}" />

After that is done you just need to type your blogs/websites URL in to any OpenID text box. Most of the OpenID authentication text boxes look like the following (with the little OpenID logo in the left of the text box):

You probably already have an OpenID authentication account that you can use right this moment. Some of the common ones that most internet users have and don’t even realize are (just replace {username}):

  • Yahoo: http://yahoo.com/
    Flickr: http://flickr.com/
    To enable your yahoo account to use OpenId visit http://openid.yahoo.com/
    Also while there check out their gallery of OpenID enabled applications
  • AOL: http://openid.aol.com/{username}
  • Blogger: http://{username}.blogspot.com/
  • Live Journal: http://{username}.livejournal.com/
  • WordPress: http://{username}.wordpress.com
  • Technorati: http://technorati.com/people/technorati/{username}

So have fun, and check out IdeaPipe at the end of this month, we will be officially supporting OpenID.