Part 12: Gzip Components like scripts, style-sheets etc

This is part 12 of a multiple part series on web performance improvements. The first Introductory blog can be found here. In previous part we discussed the impact of duplicate scripts on web page performance and how we can eliminate such silly mistakes, which can, sometimes, damage the performance a lot.


What is Gzip

Gzip reduces the size of HTTP response, which results in reduced web page response time. If an HTTP request results in a smaller response, the transfer time decreases because fewer data packets travel from server to client. This effect is even better for slower bandwidth speeds.

How Compression Works

Starting with HTTP/1.1, web clients indicate support for compression with the Accept-Encoding header in the HTTP request.

  • Accept-Encoding: gzip, deflate

If the web server sees this header in the request, it may compress the response using one of the methods listed by the client. The web server notifies the web client of this via the Content-Encoding header in the response.

  • Content-Encoding: gzip


What to compress and what not?

  • Based on compression configuration on server they choose which file type to Gzip.
  • Many web sites Gzip their HTML documents.
  • It’s also worth to Gzip your scripts and style-sheets even though they are minified.
  • Image and PDF files should not be Gzipped because they are already compressed. Trying to Gzip them not only wastes CPU resources, it can also potentially increase file sizes.
  • Gzipping has it’s own disadvantages. It takes additional CPU cycles on the server to carry out the compression and on the client to decompress the Gzipped file. To determine whether the benefits outweigh the costs you would have to consider the size of the response, the bandwidth of the connection and the Internet distance between the client and the server. This information isn’t generally available, and even if it were, there would be too many variables to take into consideration.
  • Generally, it’s worth Gzipping any file greater than 1 or 2K. The mod_gzip_minimum_file_size directive controls the minimum file size you’re willing to compress. The default value is 500 bytes.


Edge Cases

The page could easily break if either the client or server makes a mistake by sending Gzipped content to a client that can’t understand it, forgetting to declare a compressed response as gzip-encoded, etc. Mistakes don’t happen often, but there are edge cases to take into consideration.

Approximately 90% of today’s Internet traffic travels through browsers that claim to support Gzip. If a browser says it supports Gzip you can generally trust it. There are other known problems, but they occur on browsers that represent less than 1% of Internet traffic. A safe approach is to serve compressed content only for browsers that are proven to support it, such as Internet Explorer 6.0 and later and Mozilla 5.0 and later. This is called a browser whitelist approach.

Leave a Comment