Try our conversational search powered by Generative AI!

GZIP on IIS causing CSS to output symbols/garbage - intermittently

Ayo
Ayo
Vote:
 

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?

#192000
May 03, 2018 12:52
Vote:
 

Hi Ayo, out of curiosity why are you trying to implement gzip compression yourself? Why not use the IIS feature, you must have a reason why you don't want to use that?

You are not checking does the caller support gzip (request header Accept-Encoding).

And then when requesting for static files (like css, js, images, etc) those are handled by IIS and never hit asp.net (unless you remove the mappings / register your own handlers). I'm assuming your /Content/css/styles.css is actually a file on the disk and not stored to Episerver media library.

So if the styles.css is actually a static resource then check that the file encoding is the same encoding as the encoding set in web.config (default is UTF-8, defined in the globalization element).

#192033
Edited, May 03, 2018 20:12
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.