I’m trying to enable gzip compression on a site on our work intranet. Unfortunately, I don’t have access to IIS, so any changes I make have been through web.config.
The server is running IIS 6 and .NET 2.0.
I enabled compression by adding an httpmodule
public class EnableCompression : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest +=
(new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}
}
I registered in the web.config…
<system.web>
<httpModules>
<add name="EnableCompression" type="EnableCompression"/>
</httpModules>
</system.web>
Well, the above worked fine, except javascript and css files do not get compressed. From what I have found, I would have to add .js and .css to the application mappings in IIS 6, but of course I can’t do that.
Apparently this can be done via the web.config file, but I don’t know how to do that.
How can I enable compression for .js and .css files?
In IIS6, static code is not processed by managed
HttpModules; it requires a native ISAPI.One trick you can use is to convert your *.js and *.css files into dynamic files. You do this by changing them to be *.aspx, and set the ContentType to the right MIME type. For example:
The only other trick is to set
StyleSheetTheme=""in the Page directive in the markup file. Otherwise, the runtime will insist on a<head>section in the document. You can enable output caching to minimize the performance impact.I wrote a blog post about this on the JS side, in case it helps (CSS is similar, just with a different MIME type): http://www.12titans.net/p/dynamic-javascript.aspx
Unfortunately, this requires changing the name of your JS and CSS files in your app, but if you want compression and don’t have access to IIS, I don’t think there’s a way around that.
If you want to keep the *.js and *.css extensions, you can do so by adding a handler for them in your web.config. For example:
This helps from a naming perspective, but not performance; the files will still be dynamic — they are basically *.aspx files, but with a different extension. It also doesn’t work correctly with ASP.NET Themes, since pages in the Themes folder can’t be dynamic, regardless of their extension.