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.

Gravatar