Answer the question
In order to leave comments, you need to log in
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";
}
}
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question