Definition
- The browser sends an
Accept-Encodingheader listing the algorithms it supports and its order of preference; the server picks one, compresses the response body with it, and tells the browser its choice in theContent-Encodingheader (MDN). RFC 9110 saysContent-Encodinglists the codings applied, in the order they were applied, so the recipient can decode them and get the content in the type given byContent-Type(section 8.4). - In
Accept-Encodingthe browser may attach aqweight to each algorithm. If the header is absent any coding is acceptable, and if it is empty the browser wants none (RFC 9110, section 12.5.3). - MDN says the two algorithms that matter today are
gzip, the most common, andbr, which is Brotli. The gzip format in RFC 1952 is a lossless compressed format built on the DEFLATE algorithm, and can be implemented without infringing patents. RFC 7932 describes Brotli as a lossless format combining LZ77 and Huffman coding. - What gets compressed is text resources, such as HTML, CSS, JavaScript, JSON and SVG. MDN recommends turning compression on for every file except those already compressed, like images, audio and video.
Why it matters
- MDN calls compression an important way to improve a site's performance, noting that some documents can shrink by up to 70% and that text can have as much as 60% redundancy. Chrome says text-based resources should be served compressed to minimise the bytes sent over the network.
- LCP is measured from the start of navigation. What the browser downloads before it can draw the page, the HTML, CSS and JavaScript, is part of that time, so the fewer bytes it has, the shorter the download. Images are not changed by this step.
- An Arabic letter is two bytes in UTF-8, so Arabic text is larger in bytes than Latin text with the same number of letters.
- Chrome advises using Brotli because it can shrink files more than the other algorithms, with gzip as the fallback.
- The
text-compression-missingrule gzips every text response that arrived without aContent-Encoding, to measure what it would have saved, and reports only when the saving passes limits taken from Lighthouse. The rule page has the details.
Example
A request and its response, in the style of MDN's example, with the content type added:
http
GET /app.js HTTP/1.1
Accept-Encoding: br, gzip
HTTP/1.1 200 OK
Content-Type: text/javascript
Content-Encoding: br
Vary: Accept-EncodingThe Vary: Accept-Encoding header is needed: MDN says the server must send it alongside Content-Encoding, so caches can store each representation of the resource separately.
To check, open Chrome's developer tools, go to the Network tab, select the request and look at the content-encoding header among the response headers. Or from a terminal:
sh
curl -sI -H 'Accept-Encoding: gzip, br' https://example.com/ | grep -i content-encodingCommon mistakes
- Compressing what is already compressed: MDN says compressing types such as
.zipand.jpegis usually not appropriate, as it can make the file larger. - Forgetting
Vary: Accept-Encodingon the compressed response. - Compressing static files only and leaving responses generated by applications, such as JSON, uncompressed.