tags:

views:

95

answers:

1

I have implemented GZIP compression on a few of my ASP.NET pages, using a class that inherits from System.Web.UI.Page, and implementing the OnLoad method to do the compression, like so:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (Internet.Browser.IsGZIPSupported())
        {
            base.Response.Filter = new GZipStream(base.Response.Filter, CompressionMode.Compress, true);
            base.Response.AppendHeader("Content-encoding", "gzip");
            base.Response.AppendHeader("Vary", "Content-encoding");
        }
        else if (Internet.Browser.IsDeflateSupported())
        {
            base.Response.Filter = new DeflateStream(base.Response.Filter, CompressionMode.Compress, true);
            base.Response.AppendHeader("Content-encoding", "deflate");
            base.Response.AppendHeader("Vary", "Content-encoding");
        }
    }

The IsGZIPSupported method just determines whether the browser supports GZIP, looking at the Accept-encoding request header, and the browser's user agent (IE5-6 are excluded from GZIP compression). However, with this code, I am getting the web page has expired message in IE, when I postback from the page and try to use the back button. Setting the cache control to private seems to fix the problem:

base.Response.Cache.SetCacheability(HttpCacheability.Private);

But I am not sure why, or whether this will cause other problems. I haven't set any caching for any other pages in the site, and the site is running on an intranet with only a dozen concurrent users, so performance isn't a big issue at the moment.

+2  A: 

See this article on Vary header and WinInet/MSIE

It seems you should be sending Vary: Accept-Encoding instead of Vary: Content-Encoding, as the response will vary depending on the request header.

Piskvor
Should it matter that I am essentially ignoring the Accept-Encoding request header for IE6?
Dan Scott
Actually, thinking about it, it shouldn't matter because IE6 doesn't have any idea that the pages can be GZIP compressed.
Dan Scott
No, that shouldn't matter. Accept-Encoding means "I *can* deal with those encodings", but you are free to ignore this capability and serve the plain version.
Piskvor