In today’s newsletter we are going to discuss:
What is response compression
Benefits
Implementation
How to implement it
Compression providers
Compression levels
How to make custom provider
When should we compress our response
What is response compression
Response compression is a technique that can be used to reduce the size of HTTP responses.
Response compression reduces the size of HTTP response which ultimately increases the responsiveness of app.
Benefits of response compression
Pros of response compression are list below:
Improved performance
Compressing the response can reduce the amount of data that needs to be transmitted over the network, which can lead to faster page load times and a better user experience.
Reduced bandwidth usage
By compressing the response, you can reduce the amount of data that is transmitted over the network, which can lead to reduced bandwidth usage and lower costs for hosting and bandwidth
Better SEO
Search engines take page load times into account when ranking websites, so a faster loading website may rank higher in search results.
How to implement it
We can configure it using the . NET middleware —> AddResponseCompression like this:
When we set EnableForHttps = true
it can expose our requests to CRIME and BREACH attacks, so while using response compression make sure to avoid them by using anti forgery tokens when you are compressing data.
Compression Providers
.NET provides us two compression providers:
BrotliCompression
GzipCompression
By default .NET middleware uses Brotli Compression Provider
Compression Levels
Each provider has following compression levels:
Optimal
The compression operation should be optimally compressed, even if the operation takes a longer time to complete
Fastest
The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed
No Compression
No compression should be performed on the file
Smallest Size
The compression operation should create output as small as possible, even if the operation takes a longer time to complete
This is how we can set compression level for our providers:
Custom providers
Create a simple class and implement ICompressionProvider method and then you can add your CustomCompressionProvider like this in Program file:
When should we compress and when not
Responses that are not natively compressed(e.g. CSS,JS,HTML,XML,JSON) are best candidates for compression
Don’t compress natively compressed assets (e.g. PNG) and smaller files (with 150-1000 bytes)
How can I verify that my compression is working
Add middlewares, set compression levels and send request from postman and by setting different values of Accept-Encoding in header.
MIME Types Supported by Default
These are the MIME types by default supported :
text/css
text/xml
text/json
text/html
text/plain
application/xml
application/json
application/wasm
application/javascript
For more in depth knowledge you can read from Official Docs of Microsoft
Whenever you’re ready, there are 3 ways I can help you
Promote yourself to 7500+ subscribers by Sponsoring my Newsletter
Become a Patron and get access to 140+ .NET Questions and Answers
Get a FREE eBook from Gumroad that contains 30 .NET Tips (2900+ downloads)
Special Offers 📢
Ultimate ASP.NET Core Web API Second Edition - Premium Package
10% off with discount code: 9s6nuez