Enable Brotli and Gzip Response Compression in ASP.NET Core

In this article, we’ll explore how to enable response compression in ASP.NET Core applications, focusing on Brotli and Gzip algorithms and how to validate that compression works.

If you're deploying to Azure App Service on Linux, Anthony Salemo’s guide is an excellent starting point. It provides insights into compression configurations for Linux environments.

What is Response Compression?

Response compression minimizes the size of HTTP responses by employing algorithms like Brotli or Gzip. These algorithms compress text-based assets, such as HTML, CSS, and JavaScript, into smaller formats before transmitting them. Modern browsers automatically decompress the responses, ensuring a seamless experience for users.

Compression is especially critical in scenarios where limited bandwidth or high latency is a concern. Without it, web servers transmit larger payloads, which lead to slower load times.

Enable Brotli and Gzip in ASP.NET Core Middleware

To enable Brotli and Gzip compression in an ASP.NET Core application, you must configure the Response Compression Middleware.

Step 1: Verify Built-in Support

Both Brotli and Gzip are supported natively in ASP.NET Core, so no additional package installation is needed for modern versions.

However, you still need to include the using Microsoft.AspNetCore.ResponseCompression namespace in your code to access the required middleware classes and configuration options.

Step 2: Configure Middleware

Add the middleware to your Program.cs or Startup.cs file:

using Microsoft.AspNetCore.ResponseCompression;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add Response Compression services
builder.Services.AddResponseCompression(options =>
{
    options.Providers.Add<BrotliCompressionProvider>();
    options.Providers.Add<GzipCompressionProvider>();
    options.EnableForHttps = true; // Enable for HTTPS responses
    options.MimeTypes = new[] { "text/plain", "text/css", "application/json", "application/javascript", "text/html" };
});

// Configure Brotli and Gzip compression levels
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Optimal;
});

builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Optimal;
});

WebApplication app = builder.Build();

// Enable Response Compression Middleware - before app.UseStaticFiles()
app.UseResponseCompression();

await app.RunAsync();

Ensure UseResponseCompression is placed early in the middleware pipeline. This means it should appear before middleware such as UseStaticFiles or UseRouting to ensure that static files and dynamic content are compressed before being sent to the client.

Check If Compression Is Working

You can use Chrome DevTools and external tools like GiftOfSpeed to confirm that compression is working correctly.

Validation Using Chrome DevTools

1. Open your application in a browser.

2. Open Chrome DevTools (Right-click > Inspect > Network tab).

3. Reload the page to capture network traffic.

4. Click on a request (e.g., an HTML file).

5. Check the Response Headers for the Content-Encoding header. Look for:

  • Content-Encoding: gzip (for Gzip)
  • Content-Encoding: br (for Brotli)
Chrome Dev Tools: Check Gzip/Brotli Compression

Chrome Dev Tools: Check Gzip/Brotli Compression

Validation Using GiftOfSpeed

  1. Visit https://www.giftofspeed.com/gzip-test/.

  2. Enter your application’s URL.

  3. Run the test and verify that compression is enabled.

giftofspeed.com: Check Gzip/Brotli Compression

giftofspeed.com: Check Gzip/Brotli Compression

Difference Between Brotli and Gzip Compression

Brotli and Gzip are widely used compression algorithms but differ in performance and use cases.

Here’s a comparison:

Feature Brotli Gzip
Compression Efficiency Higher (smaller files) Lower
Compression Speed Slower Faster
Decompression Speed Faster Fast
Best for Static assets (HTML, CSS, JS) Dynamic content (API responses)
Browser Support Modern browsers (HTTPS only) Universal
Use Case Optimized static assets Dynamic or older browser fallback

Why Use Both?

Using both Brotli and Gzip provides:

  • Compatibility: Gzip ensures functionality for older browsers that may not support Brotli, maintaining broad accessibility.

  • Efficiency: Brotli achieves better compression ratios, resulting in smaller file sizes for static assets like HTML, CSS, and JavaScript, which improves performance on modern browsers.

For example, when serving a 100KB JavaScript file:

  • Brotli might compress it to 30KB (70% reduction).

  • Gzip might compress it to 40KB (60% reduction).

This dual approach optimizes performance across a variety of user environments.

Benefits of Response Compression

SemRush: Uncompressed pages warning

SemRush: Uncompressed pages warning

1. Improved SEO

Response compression directly influence search engine rankings.

If you're monitoring SEO for your website with tools such as SemRush or Ahrefs, you may notice a warning "Uncompressed pages" in the "Site Performance" category.

For example, SemRush provides the crystal clear tip: 

This issue is triggered if the Content-Encoding entity is not present in the response header. Page compression is essential to the process of optimizing your website. Using uncompressed pages leads to a slower page load time, resulting in a poor user experience and a lower search engine ranking.

2. Better User Experience

Reduced payload sizes result in quicker content delivery, especially on slower networks, improving overall UX.

3. Reduced Infrastructure Costs

Smaller payloads mean less bandwidth usage, saving costs in environments like Azure App Service.

4. Decreased Cumulative Layout Shift (CLS)

Faster delivery of HTML, CSS, and JS ensures page elements load predictably, reducing layout shifts.

If you're facing challenges setting up compression on IIS in Azure, check out the guide Enable Dynamic GZIP Compression for IIS Azure Web App.

This resource explains how to enable static and dynamic compression via application host configuration on IIS, but be mindful of potential CPU overheads that might cause unexpected issues.

↑ Top ↑