I have an ASP.NET MVC based ecommerce solution that gets dinged by YSlow, PageSpeed etc. for not compressing images. If I were running my own IIS server I'd set it up to use gzip compression. However, I'm running my site on shared hosting and do not have access to IIS. Is there any straightforward way of accomplishing this task using shared hosting? Thank you, JP
1 Answers
JP -this is a really big issue with shared hosting where you definitely cannot party on the root configuration. However, the good news is that you can apply ActionFilters
to get you out of the goo. As such, you can apply a little bit of GZIP tenderness to ensure that your page is transmitted in the most compact format.
I answered a question very similar to this a wee while back on SO, the details of which are here: how to gzip content in asp.net MVC?
To recap tho, here's what I suggested on this occassion.
public class CompressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(encodingsAccepted)) return;
encodingsAccepted = encodingsAccepted.ToLowerInvariant();
var response = filterContext.HttpContext.Response;
if (encodingsAccepted.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
else if (encodingsAccepted.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
}
}
usage in controller:
[Compress]
public class BookingController : BaseController
{...}
It may or may not work for your use case for the image types that you are using, but give it a try. Though, that said, there is evidence that images don't really benefit from GZIP: https://webmasters.stackexchange.com/questions/8382/gzipped-images-is-it-worth
Horses for courses I reckon and although tools like YSlow and PageSpeed are indicitive of an issue, the end result is often best left to the multitude of devices and format handlers to reconcile natively (and it's a fair bet that SOME compression may already have been applied by the server anyway, even if these tools report a problem).
To recap:
GZipping images is not worth it. The shrinkage is minuscule or even negative, because image formats do a pretty optimal job compressing already. Try zipping a JPG file to see what I mean.
Also, compressing data that is hard to compress is extremely processor intensive and you may end up using more resources rather than less.

- 1
- 1

- 22,305
- 4
- 49
- 63