Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When serving JavaScript files, is it safe to gzip it by default

The question fits in the title. I am not interested in what the spec recommend but what the mix of browsers currently deployed support the best.

  • Google Docs gzips their JS.
  • The Google AJAX Libraries API CDN gzips JS.
  • Yahoo gzips the JS for their YUI files.
  • The Yahoo home page gzips their JS.

So I think that the answer to my question is yes, it is fine to gzip JS for all browsers. But you'll let me know if you disagree.


1 Answers

If you gzip your .js (or any other content), two problems may arise: 1. gzip increases the latency for uncompressible files (needs time to compress and uncompress) 2. an older browser may not understand the gzipped content. To avoid problem 2, you should examine the Accept-Encoding and User-Agent or other parts of the HTTP request to guess if the browser supports gzip. Modern browsers should not have problems with gzippd content.

An excerpt from http://httpd.apache.org/docs/2.2/mod/mod_deflate.html: At first we probe for a User-Agent string that indicates a Netscape Navigator version of 4.x. These versions cannot handle compression of types other than text/html. The versions 4.06, 4.07 and 4.08 also have problems with decompressing html files. Thus, we completely turn off the deflate filter for them.

like image 138
2 revspts Avatar answered Feb 14 '26 18:02

2 revspts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!