I have an MVC application view that is generating quite a large HTML table of values (>20MB).
I am compressing the view in the controller using a compression filter
internal class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding))
return;
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
Is there a way to also eliminate the (quite large) amount of redundant whitespace generated in the view before I run the compress filter (to reduce compression workload and size)?
EDIT: I got it working using the WhiteSpaceFilter technique suggested by Womp below.
For interest here's the results, as analysed by Firebug:
1) No Compression, no whitespace strip - 21MB, 2.59 minutes
2) With GZIP compression, no whitespace strip - 2MB, 17.59s
3) With GZIP compression, whitespace strip - 558kB, 12.77s
So certainly worth it.
One can remove whitespace at compile time by extending Razor. That eliminates the (very significant by my measurements) runtime hit of removing white space from the generated HTML. The hit is as large as 88ms on a high end i7 trimming a 100KB document using RegEx-based code found on Stack Overflow.
The following provides an implementation of a compile-time solution for MVC 3 and MVC 4:
Meleze.Web
The solution is described at
http://cestdumeleze.net/blog/2011/minifying-the-html-with-asp-net-mvc-and-razor/
(but use the GitHub code or NuGet DLL, as the code in the blog post covers only MVC 3).
Here is a VB.NET version of a whitespace filter attribute I am using in a project:
And in Global.asax.vb:
I would say that if your View is generating over 20mb of data, you may want to investigate different ways to display the data, perhaps paging?
Whitespace compresses pretty well, I don't think removing it is going to save you much.
I would suggest trying to offload some of the HTML to the client if possible, use JavaScript to reconstitute things that repeat.
If you are returning JSON from the View, it is already minified and should not contain any whitespace or CR/LF. You should use paging to keep from sending so much data to the browser at once.
@womp has already suggested a good way of doing it but that module is pretty outdated. I have been using that but it turns out that it is not an optimal way. Here is the question I asked about:
Remove white space from entire Html but inside pre with regular expressions
Here is how I do it: