Manually enable compression using httpModule

I am trying to enable gzip compression on a site in our working intranet. Unfortunately, I do not have access to IIS, so any changes I make are made through web.config.

The server runs IIS 6 and .NET 2.0.

I turned on compression by adding 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 web.config ...

 <system.web> <httpModules> <add name="EnableCompression" type="EnableCompression"/> </httpModules> </system.web> 

Well, the above works fine, except for javascript and css files, it doesn't compress. From what I found, I would have to add .js and .css to the application mappings in IIS 6, but of course I cannot do this.

Apparently this can be done via the web.config file, but I don't know how to do it.

How to enable compression of .js and .css files?

+4
source share
2 answers

In IIS6, static code is not processed by managed HttpModules ; it requires its own ISAPI.

One trick you can use is to convert your * .js and * .css files to dynamic files. You do this by modifying them as * .aspx and setting the ContentType in the correct MIME type. For instance:

 this.Response.ContentType = "application/x-javascript"; 

The only trick is to set StyleSheetTheme="" in the "Page" directive in the markup file. Otherwise, the runtime will insist on the <head> section in the document. You can enable output caching to minimize performance impact.

I wrote a blog post about this on the JS side if that 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 application, but if you want compression and don't have access to IIS, I don't think there is a way around this.

If you want to save the * .js and * .css extensions, you can do this by adding a handler for them in your web.config. For instance:

 <compilation> <buildProviders> <add extension=".css" type="System.Web.Compilation.PageBuildProvider"/> </buildProviders> </compilation> <httpHandlers> <add path="*.css" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/> </httpHandlers> 

This helps in terms of naming, but not performance; the files will still be dynamic - these are mostly * .aspx files, but with a different extension. It also does not work correctly with ASP.NET themes, because the pages in the Themes folder cannot be dynamic, regardless of their extension.

+1
source

Adding to @RickNZ's answer:

Be careful when converting to dynamic. IIS is much faster at serving static files than dynamic (kernel mode). Keep this in mind only if bandwidth problems prevail in your application.

Another option is to look in the CDN for static content (content delivery network). Azure, Amazon, Akamai and others offer services. This is a very fast way to maintain static files convenient for geostationary.

0
source

Source: https://habr.com/ru/post/1389752/


All Articles