Compress your HTML, CSS and JavaScript before they're sent

Enable text compression was the Lighthouse audit that listed text files, such as HTML, CSS and JavaScript, sent without gzip, Brotli or deflate compression. Lighthouse 13 folded it into the Document request latency insight, which checks whether the page's own HTML arrived compressed, so check your scripts and stylesheets yourself. Compression is a server or CDN setting: the browser says which formats it accepts, and the server compresses the response and names the format it used in the Content-Encoding header.

How Lighthouse decided a file needed compression

  • Text responses only

    The old audit looked at text-based responses that came without a Content-Encoding of br, gzip or deflate.

  • Small savings skipped

    A response wasn't listed when gzip would save less than 1,400 bytes on it. Below 20,000 bytes of savings, it also had to shrink by at least 10%.

  • Savings estimated with gzip

    Lighthouse compressed each file with gzip to estimate the saving, so Brotli could save more than the figure it showed.

  • Now the HTML only

    The Document request latency insight flags the page's HTML when it arrives uncompressed, alongside redirects and a server that took more than 600 ms to respond.

The size limits come from the old audit's final source code, in Lighthouse 12.8.2, and Chrome's text compression guide describes the rest of the old audit. The Document request latency docs describe the insight, and the Lighthouse 13 release notes list the change.

Check whether a response is compressed

# Ask for Brotli or gzip and print the headers that matter
curl -sL -o /dev/null -D - -H "Accept-Encoding: br, gzip" https://yoursite.com/ \
  | grep -i -E "^HTTP/|content-encoding|vary"

# Repeat with the address of a stylesheet or script the page loads
curl -sL -o /dev/null -D - -H "Accept-Encoding: br, gzip" https://yoursite.com/app.js \
  | grep -i -E "^HTTP/|content-encoding|vary"

What the headers should say

  • A compressed response carries Content-Encoding: br or Content-Encoding: gzip. No Content-Encoding line means the file came uncompressed. If the address redirects, curl prints each response in turn, so read the lines after the last HTTP status line. MDN's compression guide adds that a server choosing the format from the request's Accept-Encoding should also send Vary: Accept-Encoding, so caches keep the compressed and uncompressed copies apart.

  • Leave images, video and other already compressed files alone. MDN's Content-Encoding reference warns that compressing formats such as JPEG again usually doesn't help and can make them bigger. Text is where it pays: web.dev's guide says gzip and Brotli can shrink larger text files by as much as 70 to 90%.

How to enable text compression

  1. Find the layer that answers requestsCompression happens wherever the response leaves: a CDN, a load balancer or the web server itself. Check that layer's settings first, since a CDN can compress files your server sends uncompressed.
  2. Turn on Brotli with gzip as the fallbackEnable Brotli for text types such as HTML, CSS, JavaScript, JSON and SVG, and keep gzip for clients that don't accept Brotli. Each request's Accept-Encoding header decides which one it gets.
  3. Compress unchanging files ahead of timeFor versioned scripts and stylesheets, compress them at build time at the highest level, Brotli 11 or gzip 9, and serve the stored copy. Responses compressed on each request do better with a middle level.
  4. Check each file type againRun the commands above for the HTML, one stylesheet and one script, and confirm each response shows a Content-Encoding header.

Frequently asked questions

Should I use Brotli or gzip?

Both, with Brotli first. Chrome's compression guide recommends Brotli because it usually makes files smaller, with gzip as the fallback. In web.dev's example, a 1,346 KiB JavaScript library came down to 256 KiB with Brotli and 329 KiB with gzip.

Why does DevTools show two sizes for one file?

With large request rows turned on in the Network panel, the Size column shows the transferred size on top and the decompressed size below. If the two are about the same for a text file, it probably came uncompressed. Chrome's compression guide walks through this check.