Modern browsers support multiple compression schemes such as deflate and gzip. Using this nifty feature in your web application can contribute greatly to an improved overall user experience. In this article, we'll discuss response (gzip) compression, it's benefits and how to implement it in your ASP.NET core web application.
A bit about response compression
Response compression is an effective way to improve the efficiency of any website. It helps reduce bandwidth use and dramatically speeds up your website's load time by reducing the overall size of files served by the web server. The procedure is simple:
- the browser requests a file.
- the web server searches through the website's directory for that file and then serves a compressed version of the file if the file is found.
- the browser receives the file and uncompresses it.
Boy!! that seems like one long process, wouldn't it take a much longer time to do all that? Well, not exactly. You see, the compressed file is usually much smaller than the uncompressed file, and serving a smaller file equates to a faster file download time by the client or web browser.
Why should I use it?
Enabling gzip compression is recommended for any web developer. Sure, you may argue that your web application works just fine without response compression, however, that's not the most efficient it can be. 100kb sure is a lot of data compared to say 12kb, for the same file. Bottom line is if you're not using any form of compression, your web pages are likely not as fast as they should be and you simply aren't maximizing their full potential. Consider this: the following pictures show how the size of a web page gets reduced to 3.0 kb from 23.1kb, and load time from 5.41s to 86ms after compression. Unbelievable right? I know!
Response compression in ASP.NET core
With the introduction of ASP.NET core 1.1 last month came the gzip compression middleware. ASP.NET core 1.1 allows you add gzip compression to the HTTP pipeline. This basically means that the Kestrel server can now do your web compression for you, instead of the front end web server. This is of critical importance to the multiplatform attribute of ASP.NET core because unlike IIS, some other web servers may not handle response compression for you.
To implement response compression in our ASP.NET web app, we first need to include the "Microsoft.AspNetCore.ResponseCompression": "1.0.0" package in our project.json file and restore packages like so.
Next we add response compression services to the ConfigureServices method in our startup class.
and finally, we add response compression to our middleware and run the application again. Really simple, isn't ?
Once your application is restarted with response compression turned on, you can check in the "Developer tools " tab of your browser (by pressing the F12 button on most browsers) and you should see right there in the "response headers" section that response compression is turned on. As shown below
Since I began using response the compression middleware in ASP.NET core I've achieved a ton of impressive performance results; sometimes getting close to a 90% compression rate as seen here.
The entire code sample used in this post can be found here
Feel free to share what the compression results on your web applications look like. Goodluck!