Send compressed static files to the browser without having to compress on demand, also has support for sending more advanced image formats when the browser indicates that i has support for it. Files need to be compressed and converted to other image formats before deploying.
This project allows you to serve precompressed files to the browser without having to compress on demand, this is achieved by compressing/encoding your content at build time.
Static nonimage files have to be precompressed using Gzip and/or Brotli, see CompressedStaticFiles.Example.csproj for an automated methodology for producing compressed css & js files.
The files must have the same filename as the source + .br or .gzip (index.html would be index.html.br for the Brotli version).
Modern browsers support new image formats like webp and avif they can store more pixels per byte.
You can convert your images using the following tools webp and libavif.
The files must have the same filename as the source but with a new file extension (image.jpg would be image.webp for the webp version).
Add AddCompressedStaticFiles() in your Startup.ConfigureServices() method.
Replace UseStaticFiles(); with UseCompressedStaticFiles(); in Startup.Configure().
By default CompressedStaticFiles is configured to allow slightly larger files for some image formats as they can store more pixels per byte, this can be disabled by calling CompressedStaticFileOptions.RemoveImageSubstitutionCostRatio().
An example can be found in the Example directory.
By using this package the Lighthouse mobile performance went from 76 to 98 and the transferred size went from 526 kb to 141 kb.
This solution is developed from a clone of AnderssonPeter/CompressedStaticFiles, which was based upon work by @neyromant from the following issue ASP.NET Issue #1584.
We built this cloned project because: