Response Compression
Enable compression to reduce bandwidth and improve load times for your ASP.NET Core applications on Plesk.
Why Use Compression?
- Reduces response size by 60-90%
- Faster page loads for users
- Lower bandwidth costs
- Better SEO rankings
Adding Response Compression
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true; // Enable for HTTPS
options.Providers.Add();
options.Providers.Add();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/json", "text/plain", "text/css", "application/javascript" });
});
builder.Services.Configure(options =>
{
options.Level = CompressionLevel.Fastest; // Or Optimal for better compression
});
builder.Services.Configure(options =>
{
options.Level = CompressionLevel.Fastest;
});
var app = builder.Build();
// Add compression early in the pipeline
app.UseResponseCompression();
app.UseStaticFiles();
// ... rest of middleware
Compression Providers
| Provider | Compression | Browser Support | Speed |
|---|---|---|---|
| Brotli | Best | Modern browsers | Slower |
| Gzip | Good | All browsers | Fast |
IIS Static Compression
IIS can also compress static files. Add to web.config:
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<httpCompression>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
</staticTypes>
</httpCompression>
</system.webServer>
Blazor WebAssembly Compression
Blazor WASM files benefit greatly from compression. Ensure your web.config has:
<staticContent>
<remove fileExtension=".br" />
<mimeMap fileExtension=".br" mimeType="application/brotli" />
<remove fileExtension=".gz" />
<mimeMap fileExtension=".gz" mimeType="application/gzip" />
</staticContent>
When NOT to Compress
- Already compressed formats (images, videos, ZIP files)
- Very small responses (<1KB)
- Real-time streaming content
Testing Compression
Check if compression is working:
- Open browser Developer Tools (F12)
- Go to Network tab
- Load your page
- Click a request and check Response Headers for
Content-Encoding: brorgzip
Best Practices
- Use Brotli for modern apps (better compression)
- Include Gzip as fallback for older browsers
- Use CompressionLevel.Fastest for dynamic content
- Use CompressionLevel.Optimal for static content
- Enable for HTTPS (most traffic)