DEV Community

Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Brotli vs. Gzip for Web Performance In Static Sites

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building *one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.

Brotli vs. Gzip for Static Sites: A Deep Dive into Web Performance

In the world of web performance, every kilobyte counts.

Faster load times lead to happier users, better SEO rankings, and improved conversion rates.

One of the most effective ways to shrink your website's footprint is through compression.

For years, Gzip has been the reigning champion, but a newer contender, Brotli, has emerged, offering even better performance.

But what exactly are they, what's the difference, and how do you ensure your static site, especially one built with Astro, is leveraging the best possible compression? Let's dive in!

What is Web Compression and Why Does It Matter?

At its core, web compression is the process of reducing the size of files (like HTML, CSS, JavaScript, and sometimes even images) before they are sent from your web server to a user's browser.

This smaller file size translates directly to:

  1. Faster Downloads: Less data to transfer means quicker delivery to the user's device.
  2. Reduced Bandwidth Costs: Especially for large sites or high traffic, saving bandwidth can mean significant cost reductions for hosting.
  3. Improved User Experience: No one likes waiting for a page to load. Faster sites mean less frustration and more engagement.
  4. Better SEO: Search engines like Google prioritize fast-loading websites, giving them an edge in search rankings.

Gzip: The Veteran Workhorse

Introduced way back in the early 1990s, Gzip (short for GNU Zip) uses the DEFLATE algorithm. It's an open standard and has been the cornerstone of web compression for decades.

How it works: When a browser requests a file, it sends an Accept-Encoding: gzip header.

If the server supports Gzip, it compresses the file on the fly (or serves a pre-compressed version), sends it with a Content-Encoding: gzip header, and the browser decompresses it.

Implications for Static Sites:
For static sites, the ideal approach is pre-compression.

You compress your files once during the build process and store the .gz versions alongside your original files.

The web server then simply serves the appropriate pre-compressed file when requested. This avoids the CPU overhead of compressing on every request, making delivery incredibly fast.

Brotli: The Modern Challenger

Developed by Google and open-sourced in 2015, Brotli (br) is a relatively newer compression algorithm.

It builds upon Gzip's principles but includes several advancements, notably a pre-defined dictionary of common words and phrases found in web content (like HTML tags, CSS properties, common JavaScript keywords).

This dictionary helps it achieve better compression ratios, especially for text-based assets.

How it works: Similar to Gzip, browsers signal Accept-Encoding: br. Servers then serve a Brotli-compressed file with Content-Encoding: br.

Implications for Static Sites:
Brotli truly shines for static sites because its main "downside" (slower compression speed at higher levels) becomes an advantage.

Since you're pre-compressing your files only once during your site's build, you can use Brotli's highest compression levels (up to 11) to achieve maximum file size reduction.

The browser's decompression speed is still very fast, so users benefit from smaller downloads without any noticeable delay.

Brotli vs. Gzip: The Key Differences

Feature Gzip Brotli
Algorithm DEFLATE LZ77, Huffman coding, and a 120KB static dictionary
Compression Ratio Good Excellent (typically 15-25% better for text files)
Compression Speed Generally faster at lower compression levels Slower at higher compression levels (but faster at lower)
Decompression Speed Fast Very fast (comparable to Gzip)
Browser Support Universal (virtually 100%) Modern browsers (over 97% global support)
Best Use Case Dynamic compression, universal fallback Static pre-compression for text assets, modern traffic

For static sites, Brotli is the clear winner in terms of file size reduction, which is the ultimate goal when assets are pre-compressed.

The Optimal Strategy: Use Both!

The best approach for any modern static site is to implement both Brotli and Gzip.

This ensures you deliver the smallest possible files to clients that support Brotli, while still providing a robust fallback for those that only support Gzip.

Here's how the negotiation works:

  1. A browser sends an Accept-Encoding header, e.g., Accept-Encoding: gzip, deflate, br.
  2. Your server (or CDN) prioritizes br (Brotli) because it's typically listed first or is configured to be preferred.
  3. If a .br version of the requested file exists, it's served.
  4. If not, or if the browser only supports gzip, the server looks for a .gz version.
  5. If neither compressed version is found, the uncompressed file is served.

This "content negotiation" ensures maximum compatibility and optimal performance.

Implementing Brotli & Gzip in Astro with astro-compressor

If you're building a static site with Astro, astro-compressor is an excellent integration that simplifies the process of pre-compressing your build output. It's an essential tool for ensuring your Astro site is as fast and efficient as possible.

Installation

First, install the package in your Astro project:

npm install astro-compressor
# or yarn add astro-compressor
# or pnpm add astro-compressor
Enter fullscreen mode Exit fullscreen mode

Basic Configuration: Enabling Brotli and Gzip

To get started, you'll need to add astro-compressor to your astro.config.mjs file. By default, it will likely enable both Gzip and Brotli, but it's good practice to be explicit.

Here's how you'd configure it in astro.config.mjs:

// astro.config.mjs
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";

export default defineConfig({
  // ... other Astro configurations
  integrations: [
    compressor({
      // Explicitly enable both for best practice and compatibility
      gzip: true,
      brotli: true,
    }),
  ],
});
Enter fullscreen mode Exit fullscreen mode

When you run astro build, this integration will automatically generate .gz and .br versions of your eligible static assets in your dist (or output) directory.

Advanced Settings: Fine-Tuning Compression Levels

For static sites, we want the best possible compression ratio, which means using higher compression levels. astro-compressor allows you to configure these directly.

  • Gzip level: Ranges from 1 (fastest, least compression) to 9 (slowest, most compression). For pre-compression, level: 9 is recommended.
  • Brotli level: Ranges from 0 (fastest, least compression) to 11 (slowest, most compression). For pre-compression, level: 11 is highly recommended. The chunkSize can also be tweaked, but the default is often sufficient.
// astro.config.mjs
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";

export default defineConfig({
  // ...
  integrations: [
    compressor({
      gzip: {
        level: 9, // Use max Gzip compression for static files
      },
      brotli: {
        level: 11, // Use max Brotli compression for static files
        // chunkSize: 16 * 1024, // Example: Adjust chunk size if needed
      },
    }),
  ],
});
Enter fullscreen mode Exit fullscreen mode

Customizing File Handling

By default, astro-compressor will compress common web assets like .css, .js, .html, .xml, .svg, and .txt. If you have specific needs, you can customize the fileExtensions array.

For example, to only compress HTML and CSS files:

// astro.config.mjs
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";

export default defineConfig({
  // ...
  integrations: [
    compressor({
      fileExtensions: [".html", ".css"], // Only compress these file types
    }),
  ],
});
Enter fullscreen mode Exit fullscreen mode

The default fileExtensions array is [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"], which covers most typical static site needs. You generally won't need to change this unless you have specific file types you want to include or exclude.

Deployment Considerations

Once you've built your Astro site with astro-compressor, your dist folder will contain your original files, plus their .gz and .br counterparts.

You still need to configure your web server or CDN to serve these correctly!

  • Netlify/Vercel/Cloudflare Pages: These platforms automatically detect and serve pre-compressed Brotli and Gzip files, usually requiring no extra configuration on your part. Just deploy your dist folder, and they handle the content negotiation.

  • Self-hosted (Nginx/Apache): You'll need to configure your server to:

1.  Check if the client's Accept-Encoding header supports br.

  1. If yes, serve the .br version and set Content-Encoding: br.
  2. If not (or if br is not available), check for gzip.
  3. If yes, serve the .gz version and set Content-Encoding: gzip.
  4. Otherwise, serve the uncompressed file.

For example, an Nginx configuration might use gzip_static on; and brotli_static on; directives, along with add_header Accept-Encoding "gzip, br"; for proper handling.

Enter fullscreen mode Exit fullscreen mode




Conclusion

For static sites, embracing both Brotli and Gzip is the gold standard for web performance.

Brotli offers superior compression ratios for modern browsers, leading to smaller downloads and faster experiences, while Gzip provides essential universal compatibility.

With tools like astro-compressor, integrating this powerful optimization into your Astro site is straightforward, paving the way for a lightning-fast web presence.

Don't leave free performance on the table – compress your static assets today!

FreeDevTools

I’ve been building for FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools

Top comments (0)