I have a MVC application that is using GZIP to compress the content before it gets to the client.
This is working, however intermittently CSS files are being returned as symbols:
[![enter image description here][1]][1]
I had a few issues getting GZIP to work but in the end I added some code to my Application_BeginRequest method to force everything to get GZIPed, the web application is running on a Content Management System, so I am avoiding any CMS related paths:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (!Request.RawUrl.ToLower().Contains(".mp4")&& !Request.RawUrl.ToLower().Contains("episerver")&& !Request.RawUrl.ToLower().Contains("app_themes")&& !Request.RawUrl.ToLower().Contains("util"))
{
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;
}
![]()
After a big of googling, I think this may have something to do with staticCompressionIgnoreHitFrequency, though I don't fully understand why.
The symbols seem to show up with I clear the cache and request the CSS file again.
can I set staticCompressionIgnoreHitFrequency in the C# code some how?
Can anyone come up with an explanation of whats going on here?
**UPDATE:**
This seems to happen only when I do hard refresh.
So I put a break point in visual studio to see if there was a difference between the request with out a hard refresh and the request with a hard refresh, and the only diffrence I could see is that the header If-Modified-Since is always null if I do a hard refesh, so I have put in an extra if statement like this:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string IfModifiedSince = HttpContext.Current.Request.Headers["If-Modified-Since"];
if (!string.IsNullOrEmpty(IfModifiedSince))
{
if (!Request.RawUrl.ToLower().Contains(".mp4")
//&& !Request.RawUrl.ToLower().Contains(".css")
//&& !Request.RawUrl.ToLower().Contains(".js")&& !Request.RawUrl.ToLower().Contains("episerver")&& !Request.RawUrl.ToLower().Contains("app_themes")&& !Request.RawUrl.ToLower().Contains("util"))
{
HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.AppendHeader("Vary", "Content-Encoding");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}
}
This seems to solve the problem, but I'm not sure if this is just a work around.... would really appreciate if someone can shed some light on this?