Pre-compressed static files with ASP.NET Core

Install gzipper

npm i gzipper -D

Add module to scripts in your package.json and run compress command npm run compress

"scripts": {
   "gzipper": "gzipper",
   "compress": "gzipper compress ./src"
 }

or Use npx command.

"scripts": {
  "compress": "npx gzipper compress ./src"
}

Serve static files

var mimeTypeProvider = new FileExtensionContentTypeProvider();
 
app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = context =>
    {
        var headers = context.Context.Response.Headers;
        var contentType = headers["Content-Type"];
 
        if (contentType != "application/x-gzip" && !context.File.Name.EndsWith(".gz"))
        {
            return;
        }
 
        var fileNameToTry = context.File.Name.Substring(0, context.File.Name.Length - 3);
 
        if (mimeTypeProvider.TryGetContentType(fileNameToTry, out var mimeType))
        {
            headers.Add("Content-Encoding", "gzip");
            headers["Content-Type"] = mimeType;
        }
    }
});

References
https://gunnarpeipman.com/aspnet-core-precompressed-files/