Automatically compress served content

I noticed that netlify doesn’t automatically compress some files that it should be able to, in particular .ttf, .woff2 fonts and .ico files. The browser I’m using has accept-encoding: gzip, deflate, br. I saw that other html files, js/css files, and .woff font files have been compressed with br, but why not the other files?

Hi, @zhjngli. Would you please send us the actual URL which is being served incorrectly?

oops, it’s at https://zhjngli.com
thanks!

Is there an update? Do you need a more specific url? thanks!

Hi, @zhjngli. I see the response sent compressed when I test:

If you are not seeing compressed responses would you please send us an example of what you are seeing? If possible, a HAR file recording of the issue happening would be ideal.

You can send the recordings via any cloud storage.

But to weigh in, I think this is done on purpose to keeps websites fast. Essentially, compression and speed go in opposite ways. The more you have to compress, the more time you need, increasing the time to load the page. It’s because, Netlify compresses the assets when they’re requested and doesn’t store compressed assets because not every request accepts compressed assets.

So, text assets like HTML, CSS, JS are easily compressed. Even then I guess, they’re not compressed with Brotli level 11, they are stuck at 7 around I suppose, as further compressing would again increase time. But the other assets that you mention take more time to compress and with probably a million websites on Netlify and even if each website has 10 images, it would make 10 million images. Imagine compressing them on the fly for every request.

I might be wrong as these are just my assumptions.

1 Like

Hi, @zhjngli. Not all files can be compressed. This is a fundamental truth of data compression. There is an explanation here:

To summarize, files which don’t compress when compression is attempted, are not modified. In other words, if “compressing” will only make the file bigger (and, yes, that is what is happening) we won’t attempt to compress it.

Don’t believe me? Well, how about some proof! :wink: :slight_smile:

$ curl -o font.woff2 'https://zhjngli.com/fonts/hind-madurai-v6-latin-300.woff2' --compressed
$ ls -l font.woff2
-rw-r--r--  1 luke  staff  14040 Jan 31 18:38 font.woff2
$ brotli font.woff2
$ ls -l font*
-rw-r--r--  1 luke  staff  14040 Jan 31 18:38 font.woff2
-rw-r--r--  1 luke  staff  14045 Jan 31 18:38 font.woff2.br

The brotli compressed file is five bytes larger. (Which, again, is actually completely normal and expected behavior in the science of data compression.) The original font file size is 14040 and it is 14045 after compression with brotli.

Again, not all files can benefit from compressions (precisely because they are already highly compressed files). If that is the case, we won’t compress it.

If there are other questions or concerns, please let us know.

1 Like