Home
Do you use Gzip?
  v9.0 Posted at 30/05/2019 5:06 PM by barrysanders@ssw.com.au
​Gzip is a file format and a software application used for file compression and decompression.
Gzip can reduce file size and storage space, and reduce transmission time when transferring files over the network. It runs on both Linux and Windows.

​Use PageSpeed Insights extension in Chrome to determine if your site will benefit from using Gzip. 

For more information about how to use PageSpeed to find which files on your site would benefit from being compressed with Gzip see Do-you-use-PageSpeed.​


Three ways to add Gzip compression to your site: ​

Use one of the methods described below to add Gzip compression to your site ASP.Net/Angular website


  • Method 1: Turn on "Dynamic Content Compression" In IIS Server. 2.pngFigure: Choose the website which you want to use Gzip and click on Compression.​​​​3.pngFigure: Install "dynamic content compression" if you haven't installed it.​

  • ​​
    In Control Panel navigate to All Control Panel Items | Programs and Features , and click Turn Windows features on or off.
    Choose Internet Information Services | Web Management Tools | World Wide Web Services | Performance Features | Dynamic Content  ​Compression.4.pngFigure : Click "Ok" to install it.5.pngFigure: now enable dynamic content compression for your site.

    • ​​​Method 2:  Using “Gzipper” in your Angular website​
    • Follow  https://www.npmjs.com/package/gzipper .(but it still need IIS Server enable static content compression.)
      Using "npm i gzipper g" to install "gzipper" first. Add to scripts in your package.json​​​​
      7.png

      6.pngFigure: "Finish configuration like that.

    • Method 3: ​​​​​​​​​​Using ASP.NET code in MVC​
    • Refer to https://www.codeproject.com/Tips/1080065/Improve-the-Performance-of-ASP-NET-MVC-Web-Applica . ​ To implement this in ASP.NET MVC, we can utilize ActionFilterAttribute and override either OnActionExecuting or OnResultExecuting method. The below code snippet is being used to check whether the current request browser can accept GZIP/DEFLATE encoding by looking at Accept-Encoding request header. If it finds GZIP encoding in this header, then we would set gzip in Content-encoding in response header and if it supports DEFLATE, then this code would set deflate in Content-encoding.
      
      using System;
      using System.Collections.Generic;
      using System.IO.Compression;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      
      namespace HTTPCompression.ActionFilters
      {
          public class CompressAttribute : ActionFilterAttribute
          {
              public override void OnResultExecuting(ResultExecutingContext 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);
                  }
              }
          }
      }​​
      
      
                                                
      
      [Compress] 
       public ActionResult About() ​
       { 
          ViewBag.Message = "Your application description"; 
          return View(); 
       } 
      ​​
      
      5.28.6.png
      ​Figure: Bad Example​, files with large size and slow load time.

      ​​​5.28.7.png

      ​​​​Figure: Good Example​, gzipped files with smaller size and faster load time.






Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: