views:

216

answers:

2

hi, I've wrote very simple minification/compression handler that minify css and js by indicating request type (Request.RawUrl.EndsWith("css" || "js")), but i don't know an approach to indicate which response type is html and then minify that as HTML-content because in mvc isn't extension to checking.

thanks in advance ;)

+1  A: 

If you wrote an HTTP handler to compress static resources it's up to you to set up the Content-Type header based on the file type:

if (Request.RawUrl.EndsWith("css"))
{
    Response.ContentType = "text/css";
} 
else if (Request.RawUrl.EndsWith("js"))
{
    Response.ContentType = "text/javascript";
}

Btw I would recommend you to minify/compress your static resources in advance and rely on the web server's gzip compression and client caching. I would avoid writing such handlers if it's not education purposes.

Darin Dimitrov
ok, but i want to detect if respnse is html output then minify/compress that as html, or if respnse is js then minify/compress that as js, or etc
Sadegh
my problem is how i can detect response is html markup
Sadegh
What response? It's your HTTP Handler that generates the response, isn't it?
Darin Dimitrov
what is another approach to doing this? why not handler?
Sadegh
Another approach is to compress/minify your static resources in advance using one of the many available tools and deploy your site with static resources already minified. From performance standpoint it's better to leave static resources handled by the web server.
Darin Dimitrov
ok, but there is also several articles thats used on the fly minification/compression like http://www.codeproject.com/KB/aspnet/combres2.aspx or http://www.codeproject.com/KB/aspnet/AspNetOptimizer.aspx etc; but i agree with your answer however can you guide me to resolve this issue? ;)
Sadegh
in combres2, author detected HTML by .aspx extension
Sadegh
For javascript files there are Dean Edwards Packer (http://dean.edwards.name/packer/), Google Closure Compiler (http://code.google.com/closure/compiler/), YUI Compressor (http://developer.yahoo.com/yui/compressor/) and hundreds of other tools. Pick one of them, feed your javascripts to it and use the result files on your site. Ideally this process should be automated as part of your continuous integration process so that when you modify the source during the build, files will be automatically compressed. You don't need to write any handlers for this. Handlers are useful for dynamic content.
Darin Dimitrov
exactly, **Handlers are useful for dynamic content** when i request mysite.com/ the output is dynamically rendered to user and i want to process this output such minify/compress. how? i have already used Packer.NET, YUI Composser, MSAJAX Minifier but no HTML targeted approach.
Sadegh
A: 

hm..., i think you misunderstood my goal/problem. here are my handler:

 public void ProcessRequest(HttpContext context)
    {
        if (Preferences.EnableHtmlMinification && **IfResponseContentIsHtml**)
        {
             //Do minify here
        }
        if (Preferences.EnableHtmlCompression && **IfResponseContentIsHtml**)
        {
            acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);
            if (acceptEncoding.Contains("gzip"))
            {
                response.Filter = new HtmlCompressStream(response.Filter, CompressionMode.Compress, HtmlCompressStream.CompressionType.GZip);
                response.AddHeader("Content-encoding", "gzip");
            }
            else if (acceptEncoding.Contains("deflate"))
            {
                response.Filter = new HtmlCompressStream(response.Filter, CompressionMode.Compress, HtmlCompressStream.CompressionType.Deflate);
                response.AddHeader("Content-encoding", "deflate");
            }
        }
        else
        {
            response.Filter = new HtmlCompressStream(response.Filter, CompressionMode.Compress, HtmlCompressStream.CompressionType.None);
        }
    }
Sadegh