Not setting up gzip or Brotli compression

Loading

The Critical Importance of Gzip/Brotli Compression for Web Performance

Failing to implement proper compression is one of the most significant yet easily fixable performance oversights in web development. Let me walk you through a comprehensive solution.

Why You Absolutely Need Compression

  1. Performance Impact: Uncompressed assets can be 4-10x larger than their compressed versions
  2. Core Web Vitals: Directly affects Largest Contentful Paint (LCP) scores
  3. Mobile Users: Crucial for users on slow networks or limited data plans
  4. SEO Ranking: Google explicitly considers page speed in search rankings

Complete Implementation Guide

1. For Static Site Hosting (Netlify/Vercel/GitHub Pages)

# Install compression plugin for your build tool
npm install compression-webpack-plugin brotli-webpack-plugin --save-dev
// Next.js config (next.config.js)
const withPlugins = require('next-compose-plugins');
const withCompression = require('compression-webpack-plugin');

module.exports = withPlugins([
  [withCompression, {
    algorithm: 'gzip',
    test: /\.(js|css|html|svg)$/,
    threshold: 8192,
  }],
  // Brotli requires Node 11.7.0+
  [withCompression, {
    filename: '[path].br',
    algorithm: 'brotliCompress',
    test: /\.(js|css|html|svg)$/,
    compressionOptions: { level: 11 },
    threshold: 8192,
  }],
]);

2. For Node.js Servers (Express)

const express = require('express');
const compression = require('compression');

const app = express();

// Enable both gzip and brotli
app.use(compression({
  // gzip by default
  filter: shouldCompress,
  threshold: 0,
  brotli: {
    quality: 11, // max compression
    params: {
      [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
    },
  },
}));

function shouldCompress(req, res) {
  // Don't compress images and other binary files
  if (req.headers['x-no-compression']) return false;
  return compression.filter(req, res);
}

3. For CDN Configuration (Cloudflare)

  1. Go to Speed > Optimization
  2. Enable Brotli and Auto Minify
  3. Set Rocket Loader to On
  4. Enable Mirage for mobile users

Critical Verification Steps

  1. Test with curl:
   curl -H "Accept-Encoding: gzip" -I https://yoursite.com | grep -i content-encoding
   curl -H "Accept-Encoding: br" -I https://yoursite.com | grep -i content-encoding
  1. WebPageTest Analysis:
  • Check “Content Breakdown” for compressed assets
  • Verify “First Byte” times improved
  1. Lighthouse Audit:
  • Run before/after tests
  • Expect 10-30% performance score improvement

Advanced Optimization Techniques

1. Pre-compression During Build

// vite.config.js
import viteCompression from 'vite-plugin-compression';

export default {
  plugins: [
    viteCompression({
      algorithm: 'brotliCompress',
      ext: '.br',
      threshold: 10240,
      deleteOriginFile: false
    }),
    viteCompression({
      ext: '.gz',
      threshold: 10240,
      deleteOriginFile: false
    })
  ]
}

2. Nginx Fine-Tuning

gzip_static on; # Serve pre-compressed .gz files
brotli_static on; # Serve pre-compressed .br files

# Compression dictionaries for better Brotli ratios
brotli_types *;
brotli_comp_level 11;
brotli_window 1m;
brotli_min_length 20;

Common Pitfalls and Solutions

  1. Double Compression:
  • Disable compression in your app if CDN handles it
  • Check content-encoding headers
  1. CPU Overhead:
  • Use level 4-6 for dynamic compression
  • Pre-compress assets during build
  1. Cache Invalidation:
   location ~* \.(js|css)$ {
     add_header Vary Accept-Encoding;
     expires 1y;
     access_log off;
   }

Real-World Impact Example

Before Compression:

  • main.js: 1.2MB
  • styles.css: 350KB
  • Total: 1.55MB transfer

After Compression:

  • main.js.br: 210KB (82% reduction)
  • styles.css.br: 45KB (87% reduction)
  • Total: 255KB transfer (84% smaller)

Result: Page load time decreased from 3.2s to 1.4s on 3G connections

Maintenance Checklist

  1. [ ] Verify compression in CI pipeline
  2. [ ] Monitor CPU usage after enabling
  3. [ ] Set up canary testing for new configs
  4. [ ] Document compression settings for team
  5. [ ] Schedule quarterly compression audits

By implementing proper compression, you’re not just optimizing your site – you’re fundamentally improving the user experience for every visitor, especially those in emerging markets or on mobile devices. The setup takes less than an hour but delivers permanent performance benefits.

Leave a Reply

Your email address will not be published. Required fields are marked *