Improve the YSlow score - remove the ETags

There are a lot of resources on the web which tell you how to create a fast website. You can measure the speed with for example YSlow. Do you want reasons? Maybe it’s good to read for example Scott Hanselman’s blog or his recent podcast in which he interviews Steve Souders the creator of YSlow.

I’m not here to discuss the reasons, but one of the things that can improve the speed of your website is to get rid of the ETags. ETags are essentially a kind of checksum that can be used to determine if a file on the server has changed. I wanted to improve my YSlow score, and part of the way to improve it was removing the ETag. But how?

Manipulate the HTTP headers through configuration?

1. Remove the header

<httpProtocol>
    <customHeaders>
        <remove name="ETag" />
    </customHeaders>
</httpProtocol>

Doesn’t work. The header is still sent.

2. Set the header explicitly to empty string

<httpProtocol>
    <customHeaders>
        <add name="ETag" value="" />
    </customHeaders>
</httpProtocol>

Doesn’t work either. The header is still sent.

What does work? A HTTP Module?

Alright, let’s try it, write a small HTTP Module and manipulate the headers before they are sent to the client. I just tried to remove the tag.

public class RemoveETagModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    }

    void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Headers.Remove("ETag");
    }
}

Of course we need to configure it. So add the below code to your web.config file.

<system.webServer>
  ...
  <modules runAllManagedModulesForAllRequests="true">
      <add name="RemoveETag" type="MM.Website.Web.Modules.RemoveETagModule, MM.Website.Web" />
  </modules>
  ...
</system.webServer>

Did it work? So let’s run Fiddler.

image

Yes, it’s gone.

Advertisement?

Yes this is kind of an advertisement, but I might end up with a set of licenses. That's why. Unit Testing ASP.NET? ASP.NET unit testing has never been this easy. Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers. The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price. Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit. The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement. Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends. Go ahead, click the following link for more information on how to get your free license.