Don't quite understand serving gzip (compressed) JavaScript and CSS files

By | January 12, 2018
Questions:

What is the point of serving compressed JavaScript and CSS files? I know its for lower file size but doesn’t the browser/webserver need to decompress it to read? I’ve read that the webserver handles the compression. Does that mean in our HTML doc we link foobar.css.gz rather than foobar.css?

How exactly does this whole compression of JS and CSS files work? Why is it useful? Are there any downsides to this? How does one accomplish this?

Answers:

The CPU time needed to decompress the HTTP response tends to be much shorter than the time it would take to transfer the extra bytes over the network.

Most systems using HTTP compression do it on the fly and do not require that publishers store compressed .gz files.

The specifics for accomplishing it depends on the web server you are using. Since you tagged this with two different servers, it is hard to say how to solve it in your case. The manual for your server should explain it though.

Questions:
Answers:

You’re right – it is a lower file size, and the browser does need to decompress it. But the lower file size while getting from the server to the browser is the important part (it saves both the server and the browser bandwidth, which usually costs money).

Decompressing such small files tends to be really fast, too, faster than downloading the extra data.

How you do it depends on your sevrer, I know for apache you can use mod_deflate, and you just have to set that up on the server and it compresses the files automatically as they’re sent, you don’t need to change any of your web pages or anything.

Leave a Reply

Your email address will not be published. Required fields are marked *