1

I have feebly attempted to enable compression for an ( ) web site that I'm working on. I thought that I had everything working, but when I got to dynamic loading of and files, they come through compressed and as such the page either fails to load content that relies on the aforementioned files or (when debugging) breaks with countless javascript runtime exceptions, i.e.; invalid character.

Here is (part of) my Global.ascx.cs code behind, obviously wired to the PostReleaseRequestState event.

void OnGlobalPostReleaseRequestState(object sender, EventArgs e)
{    
        string contentType = Response.ContentType;

        // Compress only html, style-sheet, and javascript documents.
        switch (contentType)
        {
            case "application/x-javascript":
            case "text/javascript":
            case "text/css":
            case "text/html":
                {
                    // Get the Accept-Encoding header value to know whether zipping is supported by the browser or not.
                    string acceptEncoding = Request.Headers["Accept-Encoding"];
                    if (string.IsNullOrEmpty(acceptEncoding)) return;

                    // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
                    if (acceptEncoding.Contains("gzip"))
                    {
                        // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                        Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
                        Response.AppendHeader("Content-Encoding", "gzip");
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                        Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress);
                        Response.AppendHeader("Content-Encoding", "deflate");
                    }
                }
                break;
        }
    }

And my web.config with sections of relevance.

<!-- language-all: lang-xml -->
<staticContent>
  <!-- Override IIS default, thus allowing JavaScript compression -->
  <remove fileExtension=".js" />
  <mimeMap fileExtension=".js" mimeType="text/javascript" />
</staticContent>
<httpCompression directory="%SystemDrive%\websites\_compressed" minFileSizeForComp="256">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="false" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
<httpProtocol>
  <customHeaders>
    <add name="Content-Encoding" value="gzip" />
    <add name="X-UA-Compatible" value="IE=9" />
  </customHeaders>
</httpProtocol>

I need to either disable (*.axd) compression, or force it to be decompressed on the client. Please help...

David Pine
  • 23,787
  • 10
  • 79
  • 107

1 Answers1

3

Ok, so I figured out the answer to my question... Apparently asking the question is all I needed to to do, to answer it myself. First of all, IE sucks in terms of it's determination to cache dynamic (*.axd) files, as a result I had simply had to clear IE's cache (and history) and add a simple check against the Request as depicted below.

void OnGlobalPostReleaseRequestState(object sender, EventArgs e)
{
    if (Request.RawUrl.Contains("ScriptResource.axd", StringComparison.OrdinalIgnoreCase))
    {
        return;
    }

    string contentType = Response.ContentType;

    // Compress only html, style-sheet, and javascript documents.
    switch (contentType)
    {
        case "application/x-javascript":
        case "text/javascript":
        case "text/css":
        case "text/html":
            {
                // Get the Accept-Encoding header value to know whether zipping is supported by the browser or not.
                string acceptEncoding = Request.Headers["Accept-Encoding"];
                if (string.IsNullOrEmpty(acceptEncoding)) return;

                // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
                if (acceptEncoding.Contains("gzip"))
                {
                    // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                    Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "gzip");
                }
                else if (acceptEncoding.Contains("deflate"))
                {
                    // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                    Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "deflate");
                }
            }
            break;
    }
}

As detailed here - ScriptResource.axd are automatically compressed, knowing this I can now filter out any and all requests that contain the as part of their Raw URL.

Using fiddler I was able to see that the (ScriptResource.axd) files were having two "Content-Encoding" of "gzip" in their headers, one that occurred automatically and one that I was appending.

Double compression == major headaches! :)

Community
  • 1
  • 1
David Pine
  • 23,787
  • 10
  • 79
  • 107
  • This was my exact same problem. After enabling gzip, ScriptResource responses were double gzipped. Your solution works great. – rocketsarefast Jul 08 '15 at 20:02
  • @DavidPine I'm finding it impossible to deliver gzip compressed resources whether it's .css or .axd. Even more frustrating is the fact that when I look at the IIS compressed folder I see the files there compressed, but I get no Contact Encoding: GZip in the Response Headers. In your code above you are manually adding this header... why? Wouldn't IIS do that automatically? AND, is your GlobalAsax code above required to enable compression of AXD resources, i.e. you can't do this via web.config – Jacques Jul 20 '16 at 16:37
  • @Jacques, I honestly do not remember. This was a long time ago now, and the details have since fled my recollection. I propose that you ask this as a new question and feel free to reference this Q/A in your new question. Sorry I can't be more help. – David Pine Jul 20 '16 at 16:55
  • @DavidPine Thanks David. I have a question running here http://stackoverflow.com/q/38250376/392591, where I'm trying all sorts of solutions. – Jacques Jul 21 '16 at 07:49