This post is about working with Save-Data header in ASP.NET Core. The Save-Data client hint request header available in Chrome, Opera, and Yandex browsers lets developers deliver lighter, faster applications to users who opt-in to data saving mode in their browser.
One fairly straightforward technique is to let the browser help, using the Save-Data request header. By identifying this header, a web page can customize and deliver an optimized user experience to cost- and performance-constrained users.
For this post, a middleware is getting implemented, which is looking for Image files(JPG), and it save-data header exists, it is compressing the images and delivering it.
And next you can configure extension method to add it to http pipeline.
You need to add this middleware before StaticFilesMiddleware, otherwise it won’t work properly.
If Data-Saver mode is enabled, browser will send a HTTP Request header.
The page display 5 images. And here is the network tab, without save-data mode.
And here is Network tab, after enabling the save-data header.
In this post, I am only handling JPG images, you can also exclude stylesheets and fonts etc, if it is not critical for the page. You can get more details about save-data header and various implementations here - Delivering Fast and Light Applications with Save-Data