Compression (Gzip, Brotli)

Methods for compressing text files (HTML, CSS, JS) to speed up page loads. Brotli is 15–20% more effective than Gzip.

In brief

Compression is the process of reducing the size of text resources on the server before sending them to the browser, which then automatically decompresses them. The most common algorithms are Gzip and Brotli. Enabling compression can reduce the size of HTML, CSS, and JavaScript by 60–70%, directly affecting LCP and overall page load time.

What Is Compression

Compression is a technique where the server packs a file into a more compact format before sending it to the browser. The browser receives the compressed file, decodes it, and uses it. This reduces data transfer time, especially over slow connections. For text files (HTML, CSS, JS, JSON, WOFF fonts) the saving can be 60–80%.

Gzip vs Brotli

Gzip is the standard algorithm, supported by nearly all servers and browsers. Brotli is a more modern algorithm developed by Google, offering 10–20% better compression but slightly slower compression speed. Brotli is particularly good for text and fonts.

AlgorithmCompression ratioDecompression speedBrowser support
No compression100%100%
Gzip~30–40% of originalFast99%+
Brotli~20–30% of originalMedium96%+

Modern best practice: enable Brotli for HTTPS traffic and keep Gzip as a fallback for older clients.

What to (Not) Compress

  • Compress: HTML, CSS, JavaScript, JSON, XML, SVG (lossless), WOFF/WOFF2 font files (adding extra compression), text API responses.
  • Do not compress: Already compressed formats — JPEG, PNG, WebP, MP4, WebM, ZIP (double compression offers no benefit and wastes CPU).

How to Enable Compression (Server Config)

NGINX
# Nginx: enable Brotli (requires ngx_brotli module)
brotli on;
brotli_types text/plain text/css application/json application/javascript text/xml;
brotli_min_length 1000;

# Gzip as fallback
gzip on;
gzip_vary on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1000;
APACHE
# Apache (.htaccess)
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css application/json application/javascript text/xml
  <IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI text/html text/plain text/css application/json application/javascript text/xml
  </IfModule>
</IfModule>

How to Check Compression

  • Chrome DevTools → Network → select a request → Headers → Response Headers → `Content-Encoding: br` (Brotli) or `gzip`.
  • PageSpeed Insights — will recommend enabling compression if it’s off.
  • Online services — e.g., CheckGzipCompression.com.
  • curl command — `curl -H "Accept-Encoding: br" -I https://example.com/style.css`
BASH
# Check via curl
curl -s -I -H "Accept-Encoding: gzip, deflate, br" https://example.com | grep -i content-encoding
Enabling compression is one of the simplest and most effective optimisations. In most cases it improves Core Web Vitals (LCP, FCP) and PageSpeed Insights scores.

Common questions

Brotli offers better compression (files are 15–25% smaller). Gzip compresses faster and is more widely compatible. Recommended to enable both (Brotli preferred, Gzip fallback).
You may not have configured the correct MIME types, or the file is already compressed (e.g., images). Check the Content-Encoding header.
Yes, compression applies to any text output, whether static file or generated response. Enable it at the server level for the relevant types.
Reducing the size of HTML, CSS, JS lowers load times, positively affecting LCP (large content appears faster) and FCP. Particularly noticeable on mobile devices.
Yes, JSON is text; compression reduces transfer volume, speeding up AJAX requests and improving perceived performance.
Direct contacts

Discuss your project?

Share your goals and website context — I will suggest a practical next step.

Compression (Gzip, Brotli) — What is it?