Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Network and Caching

Compression, HTTP/2 and HTTP/3

Learn how Gzip and Brotli compression shrink text assets, how minification differs, and how HTTP/2 multiplexing and HTTP/3 over QUIC make requests cheaper.

Two upgrades speed up almost every site without touching its design: compression, which sends fewer bytes, and modern protocols, which move those bytes more efficiently. Both are usually server or CDN configuration rather than code changes.

Compression

Text-based files, HTML, CSS, JavaScript, JSON, SVG and XML, contain a lot of repetition. Compression algorithms exploit it and can shrink them by 70 to 90 percent.

The browser advertises what it supports:

HTTP
Accept-Encoding: gzip, br, zstd

The server picks one and tells the browser:

HTTP
Content-Encoding: br
Vary: Accept-Encoding

Vary: Accept-Encoding tells caches to store the compressed variants separately.

Algorithm Notes
gzip Universal and fast. A solid default.
Brotli (br) Typically 15 to 25 percent smaller than gzip at high levels. Used over HTTPS by all modern browsers.
Zstandard (zstd) Fast with good ratios; supported in current browsers and growing on servers and CDNs.

Static vs dynamic. For static files, compress ahead of time at the highest level during the build and serve the saved copy. For dynamic HTML, use a faster level (Brotli 4 to 5, gzip 6), because compressing costs CPU on every request.

Do not compress files that are already compressed: JPEG, PNG, WebP, AVIF, video, audio, fonts in WOFF2 and ZIP files. It wastes CPU for no benefit.

Compression in action

Node's built-in zlib is not available in the browser sandbox, but the browser has a CompressionStream API that supports gzip. This demo compresses repetitive text and compares sizes.

JS Gzip a repetitive string

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Highly repetitive markup compresses extremely well, which is why bloated HTML is less expensive on the wire than it looks, though the browser still has to parse it.

Minification

Minification removes whitespace, comments and shortens names in CSS and JavaScript before compression. It reduces size and parse time. Compression and minification stack: always do both.

JS What minification removes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

This is a toy minifier. Use real tools (Lightning CSS, esbuild, Terser, cssnano) in production, since they handle edge cases safely.

HTTP/1.1, HTTP/2 and HTTP/3

Feature HTTP/1.1 HTTP/2 HTTP/3
Transport TCP TCP QUIC (over UDP)
Requests per connection One at a time Many in parallel (multiplexing) Many, with independent streams
Header compression None HPACK QPACK
Head-of-line blocking Per connection At TCP level Removed between streams
Connection setup TCP + TLS (2 to 3 round trips) Same 1 round trip, 0 for resumed

HTTP/2 lets one connection carry many requests at once, so you no longer need six connections or domain sharding. It also compresses repeated header data.

HTTP/3 replaces TCP with QUIC. A lost packet in TCP stalls every stream on the connection; in QUIC it only delays the stream it belongs to. This helps most on lossy mobile networks. QUIC also combines transport and TLS setup, saving a round trip, and survives network changes (such as moving from Wi-Fi to cellular).

Enabling them is typically a one-line setting on your CDN, or listen 443 ssl http2; and an HTTP/3 module in your web server. Browsers discover HTTP/3 through an Alt-Svc header after the first visit.

flowchart LR
  A["HTTP/1.1: queued requests"] --> B["HTTP/2: multiplexed on TCP"]
  B --> C["HTTP/3: multiplexed on QUIC"]

Each generation reduces waiting between requests.

HTTP/2 server push was removed from browsers. Use <link rel="preload"> and 103 Early Hints instead (see resource hints).

Checking what you use

In DevTools, add the Protocol column in the Network panel: you will see h2 or h3. In code, performance.getEntriesByType("navigation")[0].nextHopProtocol returns the same.

Read the protocol the page used

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which files should NOT be compressed again with gzip or Brotli?

    They are already compressed, so extra compression wastes CPU for tiny gains.
  2. 2. Which header tells the server what the browser can decode?

    Accept-Encoding lists the algorithms. The server answers with Content-Encoding.
  3. 3. What problem does QUIC in HTTP/3 reduce?

    Streams are independent, so one lost packet does not stall all the others.
  4. 4. How should you compress static files in production?

    Ahead-of-time compression gets the best ratio with no per-request CPU cost.

Summary

  • Compress HTML, CSS, JS, JSON and SVG with Brotli or gzip; skip already compressed formats.
  • Minify code first, then compress; precompress static files at the highest level.
  • HTTP/2 multiplexes requests; HTTP/3 uses QUIC to avoid TCP's head-of-line blocking and speed up setup.
  • Check the Protocol column and nextHopProtocol to confirm.

Next: Resource Hints and Priorities