1. Home
  2. Guides
  3. Compression & caching
Performance guide

Compression and caching: the cheapest speed wins

Two server settings do more for page speed than almost anything you can do in the page itself: compressing text before it is sent, and telling browsers to keep files so they do not download them again.

Key points

  • Compress text (HTML, CSS, JavaScript, JSON, SVG) with Brotli or gzip. Typical saving: 60 to 80%. Images are already compressed.
  • Give fingerprinted static files a long Cache-Control lifetime with immutable. Give HTML validators (ETag or Last-Modified).
  • Turn on HTTP/2, and HTTP/3 if your host or CDN offers it. Both are usually one setting.

Compression

When a browser asks for a page it sends Accept-Encoding: gzip, deflate, br. If the server supports one of them it sends the file compressed and says so in Content-Encoding. The browser unpacks it. Text compresses extremely well, so a 300 KB script often travels as 80 KB.

  • gzip is universal and fast. It is the safe default.
  • Brotli (br) compresses text roughly 15 to 20 percent better than gzip and is supported by every current browser over HTTPS.
  • Zstandard (zstd) is newer and supported by recent browsers. It is an optional extra.

Do not compress already compressed formats such as JPEG, PNG, WebP, video or ZIP. It wastes CPU for no gain.

Remember Vary

Because one URL can be sent in several encodings, responses should carry Vary: Accept-Encoding. Otherwise a shared cache may store a compressed copy and hand it to a client that asked for plain text.

Browser caching

The Cache-Control header tells the browser how long it may reuse a file without asking again.

  • Fingerprinted static files (app.4f9c2.js, names that change when content changes): Cache-Control: public, max-age=31536000, immutable. A year is safe because an update gets a new name.
  • Files with fixed names: a shorter lifetime such as a day, or no-cache with an ETag so the browser revalidates cheaply.
  • HTML: either a short max-age or revalidation. With an ETag or Last-Modified header the server can answer "not modified" in a few bytes.
  • Never use no-store for public static files. It forces a full download every time.

HTTP/2 and HTTP/3

HTTP/1.1 loads files one after another per connection. HTTP/2 multiplexes many requests over one connection, which helps pages with many small files. HTTP/3 runs over QUIC and recovers faster on poor mobile networks. Browsers only use them over HTTPS, and most hosts and CDNs enable them with one switch.

Turning it on

Nginx:

gzip on;
gzip_vary on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript image/svg+xml;

location ~* \.(css|js|woff2|png|jpe?g|webp|avif|svg)$ {
    expires 1y;
    add_header Cache-Control "public, max-age=31536000, immutable";
}

Apache: enable mod_deflate and mod_headers, add AddOutputFilterByType DEFLATE text/html text/css application/javascript, and set Header set Cache-Control for static file types. On a CDN such as Cloudflare, switch on Brotli and set a browser cache TTL in the dashboard.

Check the result, not the setting. CDNs and plugins often compress only some file types, and a caching rule can override a header you set on the origin server.

See also: Page weight explained, HTTP headers explained.

FAQ

Does compression cost server performance?

A little CPU, which is small compared with the bandwidth saved. Static files can be compressed once and stored, so the cost is paid only at build time.

Why is my CSS not compressed although HTML is?

Many servers compress only a default list of types. Add text/css and application/javascript to the list of compressed types.

How do I update a file that is cached for a year?

Change its name when its content changes, for example by adding a content hash. The HTML then points at the new name.

Is a CDN needed for this?

No. It is a server setting. A CDN makes it easier and adds global delivery, but a single server can do all of it.

WebsiteHealthLabs Editorial
Guides explain audit results in plain English. Facts are checked against public documentation from Google, W3C and the browser vendors, and we update them when guidance changes.

↑ ↓ to moveEnter to openEsc to closeAll results