4

I ran into a dilemma lately as I was exploring the various plugins for gulp. One of them was gulp-gzip and till then, I have never thought about compressing my files. I got gulp-gzip to work correctly and spit out gzipped versions of my HTML, CSS and JS files but then, what next?

I googled around and found that most articles talk about configuring the server to send gzipped versions of the content automatically to the client upon request. But then, I kind of don't seem to understand the purpose of gzipping locally.

So, my questions are:

  1. Can I serve gzipped content I get from gulp-gzip without configuring my server?
  2. If yes, how should I proceed -- what should I name my gzipped files as? Should I keep the .gz extension and link to my CSS and JS files using the same?
  3. If yes, can I test it locally by linking to the same .gz files?
  4. If no, what is the purpose of gulp-gzip in a development environment if the server can be configured to do it automatically?
sangeeth96
  • 1,202
  • 1
  • 11
  • 20

1 Answers1

0

Most servers have an option to serve statically pre-compressed files if a *.gz version exists, i.e. when user requests foo.css, the server will check if foo.css.gz exists and use it.

It requires server support (the server has to set appropriate HTTP headers), so it won't work with the file:// protocol and may not work on every server.

In URLs you have to refer to the base filename (do not link to .gz directly).

Compressing files ahead of time may be better:

  • You can use higher compression level (e.g. maximum gzip level or the Zopfli compressor), which would be too slow to do real-time on the server.
  • Compressing ahead of time saves CPU time of the server, because it doesn't have to dynamically compress files when they're requested.

Just be careful when you deploy files to the server to update both *.css and *.css.gz at the same time, otherwise you may be surprised that you sometimes see old version of the file.

Kornel
  • 97,764
  • 37
  • 219
  • 309