Compression and Transfer Encoding
Shrink what crosses the wire using gzip and Brotli compression, the right content types, and precompression so responses arrive faster on every connection.
Compression and Transfer Encoding is a free Web Performance Optimization & Lighthouse lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web Performance Optimization & Lighthouse learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Compress?
Text assets like HTML, CSS, JS, JSON, and SVG are highly compressible. Compressing them before transfer can cut payloads by 70-90%, directly speeding up load on every network.
How Negotiation Works
The browser advertises supported algorithms with the Accept-Encoding header; the server responds with the chosen one in Content-Encoding.
Accept-Encoding: br, gzip
Content-Encoding: brgzip
gzip is universally supported and a safe baseline. It offers good compression with low CPU cost and is enabled on virtually every server and CDN.
Brotli
Brotli (br) typically compresses text 15-25% smaller than gzip. It is supported in all modern browsers over HTTPS, making it the preferred choice today.
Compress the Right Types
Compress text-based MIME types only. Already-compressed binaries like JPEG, PNG, WebP, MP4, and woff2 fonts gain nothing and waste CPU if recompressed.
Static Precompression
For static assets, precompress at build time at the highest level. The server then serves the .br or .gz file directly, avoiding per-request CPU.
# nginx serving precompressed files
brotli_static on;
gzip_static on;Dynamic Compression Levels
For dynamic responses, use a lower compression level so CPU cost does not delay the response. High levels are best reserved for static, build-time compression.
Express Example
In Node, the compression middleware adds gzip/Brotli to dynamic responses with sensible defaults.
const compression = require('compression');
app.use(compression());Verifying Compression
Inspect a response in the Network panel: check Content-Encoding: br or gzip and compare transferred size to resource size. Lighthouse flags uncompressed text assets.
Compression and HTTPS
Modern compression for sensitive content is served over HTTPS, which also avoids historic attacks. Brotli in browsers is only offered on secure connections.
Strategy Summary
- Prefer Brotli, fall back to gzip.
- Compress text types only.
- Precompress static assets at max level.
- Use lower levels for dynamic responses.
Quick Check
You enable maximum-level compression on dynamically generated API responses and notice slower responses under load. What is the better approach?
Recap
You learned to shrink transfers with gzip and Brotli, negotiated via Accept-Encoding. Compress text types only, precompress static assets at max level, use lower levels for dynamic responses, and verify with the Network panel.
Frequently asked questions
Is the “Compression and Transfer Encoding” lesson free?
Yes — the full text of “Compression and Transfer Encoding” is free to read here on the web, and the Web Performance Optimization & Lighthouse course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web Performance Optimization & Lighthouse course, upgrade to CoddyKit PRO.
What will I learn in “Compression and Transfer Encoding”?
Shrink what crosses the wire using gzip and Brotli compression, the right content types, and precompression so responses arrive faster on every connection. You practise Web Performance Optimization & Lighthouse with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Web Performance Optimization & Lighthouse?
No prior experience is required. Web Performance Optimization & Lighthouse on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Compression and Transfer Encoding” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Web Performance Optimization & Lighthouse lesson?
Yes. Every Web Performance Optimization & Lighthouse lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- HTTP/2 and HTTP/3 Fundamentals
- Browser and Server Caching
- Content Delivery Networks (CDNs)
- Compression and Transfer Encoding