What is HTTP compression?
Useful first: HTTP · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALExplain why a compressed response can be smaller without removing part of the page.
Send fewer bytes, keep the content
Pack the text for the journey
A web server can encode a response using a lossless compression format, such as gzip or Brotli. The browser decodes it before using the content. Think of packing a blanket into a smaller bag: the package changes, but you still want the whole blanket when it comes out.
Imagine a text response that takes 100 KB before compression and 35 KB after compression. Those are invented sizes for this example, not a typical ratio or a measurement of this site. Decoding restores the original text; compression does not summarize the article.
[1] [3]Smaller is useful, but measure the outcome
Saving transfer bytes can help, but compression and decompression require processing. A JPEG or ZIP file is already compressed, so another layer may save little or even make it larger. A server can choose an uncompressed response when that is acceptable to the client.
For example, a smaller text response and an unchanged photo may both be sensible results. Do not infer the whole page's loading time from one file's byte count.
[1] [2]Compression can reduce transfer size without shortening the content; the actual benefit depends on the response and the work involved.
HTTP compression makes a text page smaller by throwing away sentences.
Lossless compression changes how the data is represented. Decoding recovers the original content, including those sentences.
Show the explanation
The encoded response uses fewer bytes, but total page-loading time still needs measurement. The size comparison concerns this response. Lossless decoding restores its text, and that one byte count does not measure the rest of the page or its total loading time.
YOUR GOALSeparate acceptable encodings, the chosen encoding and the underlying media type.
Read the request and response headers
The client offers choices
A request can include Accept-Encoding: gzip, br. Here br names Brotli. The header advertises acceptable response encodings; it is not an instruction to gzip every response. The server considers what it supports and whether applying compression is appropriate.
identity means no content encoding. An unencoded representation is normally acceptable unless the request explicitly excludes it. A listed encoding with q=0 is unacceptable.
Type and encoding answer different questions
For a fictional HTML response, these headers describe different properties:
Content-Type: text/html; charset=utf-8
Content-Encoding: gzipContent-Type identifies the media type before content encoding: the decoded content is HTML. Content-Encoding tells the recipient which decoding is needed. If Content-Length is present for this encoded response, it describes the encoded bytes, not the decoded HTML length.
Read the HTTP request and response guide for the wider exchange. These are selected headers, not a complete response with its body.
[3] [4]A compressed file is a separate case
A ZIP download already has compression built into its file format. That alone does not mean its HTTP response should declare Content-Encoding: gzip. That header would describe an additional applied content coding, not merely the fact that the file is an archive.
When debugging, inspect the actual response header rather than guessing the encoding from a filename. An HTML page can be gzip-encoded while keeping its ordinary .html address.
Accept-Encoding offers choices, Content-Encoding describes applied coding, and Content-Type identifies the decoded media.
Accept-Encoding: gzip guarantees a gzip response.
It declares that gzip is acceptable. An acceptable unencoded response can still be returned; Content-Encoding describes what was actually applied.
Show the explanation
No; an acceptable unencoded response is still possible. Accept-Encoding expresses acceptable choices. With identity still acceptable, the server can send an unencoded response; the filename does not negotiate content encoding.
YOUR GOALDiagnose cache selection and validator mistakes when one URL can return different encodings.
Keep compressed cache variants consistent
Make the selecting request field visible
Suppose /guide.html can return gzip or an unencoded representation depending on Accept-Encoding. Return Vary: Accept-Encoding consistently, including on the default response and relevant 304 responses. This tells caches which request field affected selection.
A stored gzip response from a different accepting request must not be reused without revalidation when the selecting fields do not match. For example, a later request with Accept-Encoding: identity, gzip;q=0 must not simply receive the old gzip body. Vary does not set a freshness lifetime or independently grant permission to cache; those are separate HTTP caching decisions.
Strong ETags identify the encoded variant
The same decoded HTML can have different representation bytes when encoded. Its gzip and unencoded representations therefore need distinct strong ETags, such as "guide-r4-gzip" and "guide-r4-plain". These example labels are opaque identifiers, not a prescribed naming scheme.
Using one strong ETag for both merely because their decoded text matches is incorrect. A weak tag, marked W/, has different comparison semantics and is not a substitute for byte-level identity. ETags also do not replace Vary.
Validate the representation that can be used
When a stored response needs validation, a client can send its ETag in If-None-Match. A matching selected representation can be confirmed with 304 Not Modified instead of resending its body. Keep the negotiation and variant metadata consistent through this exchange.
For a practical check, compare an accepting request and an identity-only request: inspect Content-Encoding, Content-Type, Vary and the validator, then verify the decoded content. If a CDN also serves the response, inspect the final response a reader receives. A correct origin header alone is not a measurement of the final delivery path.
Treat encoding selection, Vary matching, freshness and validators as separate parts of correct cache behavior.
If two variants decode to the same HTML, they can share the same strong ETag.
Strong validators distinguish representation bytes. Content-encoded and unencoded variants need different strong ETags even when their decoded content matches.
Show the explanation
Use distinct strong ETags for the two representations and preserve the relevant Vary policy. The encoded representation bytes differ. Distinct strong ETags avoid confusing those variants, while Vary still tells caches which request fields affect response selection.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- MDN — Compression in HTTP
- MDN — Accept-Encoding header
- MDN — Content-Encoding header
- MDN — Content-Type header
- MDN — Vary header
- MDN — ETag header
- RFC 9110 — Content-negotiated entity tags
- RFC 9111 — Cache keys and Vary