CompressedStaticFiles 2.0.0

This will ensure that you application will serve the smallest files without any server overhead, files need to be compressed and converted to other image formats before deploying.

Install-Package CompressedStaticFiles -Version 2.0.0
dotnet add package CompressedStaticFiles --version 2.0.0
<PackageReference Include="CompressedStaticFiles" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CompressedStaticFiles --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CompressedStaticFiles, 2.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install CompressedStaticFiles as a Cake Addin
#addin nuget:?package=CompressedStaticFiles&version=2.0.0

// Install CompressedStaticFiles as a Cake Tool
#tool nuget:?package=CompressedStaticFiles&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Table of Contents

About The Project

This project allows you to server precompressed files to the browser without having to compress on demand, this is achieved by compressing/encoding your content at build time.

Getting Started

Precompress content

Static nonimage files have to be precompressed using Zopfli and/or Brotli, see the example for how to do it with gulp.
The files must have the exact same filename as the source + .br or .gzip (index.html would be index.html.br for the Brotli version).

Encode images

Modern browsers support new image formates 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).

ASP.NET

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().

Example

A 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.

Acknowledgements

This solution is based on @neyromant from the following issue https://github.com/aspnet/Home/issues/1584#issuecomment-227455026.

Table of Contents

About The Project

This project allows you to server precompressed files to the browser without having to compress on demand, this is achieved by compressing/encoding your content at build time.

Getting Started

Precompress content

Static nonimage files have to be precompressed using Zopfli and/or Brotli, see the example for how to do it with gulp.
The files must have the exact same filename as the source + .br or .gzip (index.html would be index.html.br for the Brotli version).

Encode images

Modern browsers support new image formates 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).

ASP.NET

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().

Example

A 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.

Acknowledgements

This solution is based on @neyromant from the following issue https://github.com/aspnet/Home/issues/1584#issuecomment-227455026.

Release Notes

2.0.0
Added support for alternative image formats
Added support for .net 5
Removed support for .net core 2.1
1.2.0
Added support for .net core 3.1
1.1.0
New feature: Ability to specify a custom FileProvider (now using the FileProvider provided in the StaticFileOptions if it is set)
1.0.4
Converted to .NET Standard 1.6
1.0.3
Fixed issue that disabled caching parameters.
1.0.2
Added support for IIS.
1.0.1
Added logging
Picks original file if its the smallest
Added support for .NETFramework 4.5.1

1.0.0
Initial release

  • .NETCoreApp 3.1

    • No dependencies.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.0 10,201 11/11/2020
1.1.0 14,044 4/20/2020
1.0.4 37,285 10/19/2018
1.0.3 13,369 4/11/2017
1.0.2 6,673 8/22/2016
1.0.1 636 7/28/2016
1.0.0 626 7/20/2016