Advanced

Compression, minification, and what not to compress

Text compresses well because it repeats. Already-compressed formats do not, and trying to compress them again costs CPU for no bytes. Knowing which files are which is a two-minute check that saves a whole misconfiguration.

What you will see

A content-encoding line naming br or gzip. Missing means the bytes are uncompressed on the wire.

About 1 min of reading

Try It

Configure compression for the right types

1

Check one text asset

ls -la public/css/main.css; curl -s -H 'Accept-Encoding: gzip' -o /dev/null -w '%{size_download}\n' http://localhost:3000/public/css/main.css?v=20261010

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The transferred number should be well under the file size.
2

Check a font

ls -la public/fonts/ | head -3; curl -s -H 'Accept-Encoding: gzip' -o /dev/null -w '%{size_download}\n' http://localhost:3000/public/fonts/body.woff2

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Roughly equal to the file size. woff2 is already compressed, so growth or no change is correct.
3

Set the type list

grep -nE 'text/html|text/css|application/javascript' server.js middleware/*.js | head -5

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The place that decides which types compress. Add the missing text types and keep the binary ones out.
4

Confirm the header

curl -s -D - -o /dev/null -H 'Accept-Encoding: br' http://localhost:3000/public/js/main.js?v=20261009 | grep -i 'content-encoding'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
A content-encoding line naming br or gzip. Missing means the bytes are uncompressed on the wire.
Do not compress a response that is already small and unique, like a one-line redirect. The CPU cost can exceed the bytes saved.

Data Flow

File to a wire variant
1 Check the content type against the list
↓
2 Compressible: produce a br or gzip variant
↓
3 Already compressed: serve as-is
↓
4 Send the encoding header
Why this works
The type list to serve

Compress html, css, js, svg, json, and text. Exclude woff2, woff, jpeg, png, gif, mp4, and zip. That list is short enough to keep in one place.

How to verify it worked

Request with an Accept-Encoding header and compare the transferred size to the file on disk. A smaller number is the only proof.

Examples

Compress at the server, not the editor

Serve a gzip or brotli variant of the text asset so the source stays readable. The editor keeps the readable file and the server keeps the wire form.

Skip the already-compressed

woff2, jpeg, png, and mp4 are already compressed. Running gzip on them changes nothing and can grow the file. Set the type list, not a wildcard.

Minify for a different reason

Minification removes names and whitespace, which helps the compressor find less repetition to store. It is a build step, and it is not a substitute for compression.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Deferring work that is not needed yet
Previous Lesson
Deferring work that is not needed yet
Next Lesson
A budget that a person can act on
A budget that a person can act on →