A
A
Alexander Bogdanov2019-08-10 20:08:36
ASP.NET
Alexander Bogdanov, 2019-08-10 20:08:36

How to add Content-Encoding header to gzip files in ASP.NET?

I would like to give the client compressed files of scripts and styles. I configured webpack to build .gz files, but the problem is that the browser eventually cannot parse them. I found out that a special header is needed, but in my case you can't just set it "globally", since the HTML files themselves are not compressed.

app.UseStaticFiles(new StaticFileOptions {
   OnPrepareResponse = content => {
     if(content.File.Name.EndsWith(".js.gz")) {
       content.Context.Response.Headers["Content-Type"] = "text/javascript";
       content.Context.Response.Headers["Content-Encoding"] = "gzip";
     }
     if(content.File.Name.EndsWith(".css.gz")) {
       content.Context.Response.Headers["Content-Type"] = "text/css";
       content.Context.Response.Headers["Content-Encoding"] = "gzip";
     }
   }
 });

I found this option for adding headers only to statics, but it does not work. All other options on the Internet that I was able to find are similar or even copy this one. As a result, the browser receives gz files but cannot understand them
5d4ef9da6b1ae003737182.png
. Maybe someone has come across a similar one? I really want to optimize resource loading

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Pikat, 2019-08-11
@PavelPikat

You are not doing that. You don't need to do any gz files, the server will do everything for you, just enable static compression in IIS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question