PreCompressStatic 1.0.0

dotnet add package PreCompressStatic --version 1.0.0                
NuGet\Install-Package PreCompressStatic -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PreCompressStatic" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PreCompressStatic --version 1.0.0                
#r "nuget: PreCompressStatic, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PreCompressStatic as a Cake Addin
#addin nuget:?package=PreCompressStatic&version=1.0.0

// Install PreCompressStatic as a Cake Tool
#tool nuget:?package=PreCompressStatic&version=1.0.0                

PreCompressStatic

Compress once by demand if required and send preCompressed static files to the browser

NuGet version Nuget

How to use

Add app.UsePreCompressStaticFiles() instead of app.UseStaticFiles() in Startup.Configure section

namespace Example
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor(); // it's important to add
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseDeveloperExceptionPage();
            app.UseRouting();

            app.UsePreCompressStaticFiles(); // add this instead of app.UseStaticFiles();

            app.UseEndpoints(e => { e.MapFallbackToFile("index.html"); });
        }
    }
}

Rules

  • UI part can be compressed during the building (for example with webpack). Compressed files must end with .br & .gzip (for example main.js.br)
  • If browser supports Brotli or Gzip compression (Accept-Encoding header) then the helper looks for {file}.br or {file}.gzip
  • If files missed the helper compresses requested file, saves it in the same folder and returns compressed result
  • Compression happens only once per file and only if fileSize < options.MinSizeKb (8kb by default)
  • Middleware is highly optimized for performance and compresses only once if it's really makes sense

Example

An example can be found in the Example directory.

Acknowledgements

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

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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 Downloads Last updated
1.0.0 725 1/3/2024

1.0.0
     Initial release