Text compression is a method that speeds up the download of resources like HTML, CSS, and JavaScript files on your website.
In some instances, compression lets you reduce file sizes by around 70%, resulting in faster page loads and a better user experience.
In this guide, we'll show you how to enable text compression for your own website and check your current compression status.
Should you use text compression on your website?
Yes! When someone visits your website, their browser needs to download all the necessary files - HTML, CSS, JavaScript, and more. These text-based files can get quite large, especially for modern web applications.
Text compression works by applying tools like Gzip or Brotli to reduce file sizes before they're sent over the network. The browser then decompresses these files on the user's device.
The benefits of text compression include:
- Faster load times: Smaller files download quicker, which can improve your Core Web Vitals
- Reduced bandwidth usage: Less data transferred means lower costs for both you and your users
- Improved SEO: Page speed is a ranking factor for search engines
These downsides of text compression are minimal and should not deter you from using text-based compression.
- Increased CPU usage on the server: Compression requires some extra processing power, but on almost any server, this is completely negligible
- Increased CPU usage on the client: Decompressing files on low-powered devices might take a bit longer (in milliseconds), but the overall impact is very small
We're not listing missing browser support for modern compression algorithms as a disadvantage. That's because browsers advertise their support for these compression algorithms with the Accept-Encoding header.
If the server determines that the browser doesn't support the specified algorithm, it can fall back to alternative compression methods.
Types of Text Compression
GZIP and Brotli compression are supported in both modern browsers and most server environments that run your website.
Consider these two tests, done on unminified versions of react-dom.js and animate.css:
In this example, Brotli compression reduces the size of script.js by 82.7%, while Gzip compression reduces the size of style.css by 88.9%.
This doesn't mean Gzip is always better than Brotli. Brotli is generally more effective at compressing files, but in this instance, the CSS file was more compressible than the JavaScript file.
Gzip Compression
Gzip is the most widely supported compression algorithm, compatible with virtually all browsers.
Gzip works by:
- Finding repeated strings within a text file
- Replacing them with pointers to the first instance
- Applying additional encoding for further compression
Brotli Compression
Brotli is a newer compression algorithm developed by Google that offers even better compression ratios than Gzip - typically 10-20% smaller.
Brotli uses more advanced compression techniques, including:
- A pre-defined dictionary of common web patterns
- Better entropy encoding
What is the difference between minification and compression?
Minification and compression are two different techniques for reducing the size of text files on your website.
Minification example in JavaScript
Minification removes unnecessary characters from text files, and depending on the file type, can even involve renaming variables to shorter names. Minification happens before compression.
Before minification: Regular JavaScript authored by a developer.
// Before minification
function add(argument1, argument2) {
return argument1 + argument2;
}
After minification: Minified code, where variable names are renamed.
// After minification
const add=(a,b)=>a+b;
Compression example in CSS
Compression reduces the size of files by finding repeated patterns and replacing them with shorter representations. Compression happens after minification.
Before compression: Regular CSS authored by a developer.
/* Tiny CSS example */
.box {
color: blue;
padding: 5px;
}
.btn {
color: blue;
padding: 5px;
}
After compression: Compressed code that is not valid CSS but is smaller in size.
/* This is not valid CSS code */
.box { #CB; #P5; } .btn { #CB; #P5; }
Pattern Replacements: The client, for example a browser, is able to decompress the compressed file and replace the patterns with the original values.
| Pattern | Replacement |
|---|---|
color: blue; | #CB |
padding: 5px; | #P5 |
When should you use text compression?
Text compression should be applied at the server level during the response phase, before files are sent to the client.
In addition to using compression at the server runtime, you should also use a CDN that supports text compression.
Using compression during the server response phase and during the CDN delivery phase are the most common ways to use text-based compression. However if you have very specific requirements, you can also compress files during the build phase.
This method is similar to how images are optimized during the build phase.
This means you compress files before deployment as part of your build process. You can use the underlying gzip and brotli (refer to the installation instructions of those tools) to perform this compression.
How to Verify Compression is Working
Before and after enabling compression, it's important to verify that it's actually working. Here are a few ways to check:
