Skip to content

Commit 8edea81

Browse files
author
kajan
committed
configure static files middleware to set Content-Type, Content-Encoding repsonse headers for gzipped resources accordingly
1 parent 140436b commit 8edea81

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

AspNetCoreVueMpa.Web/Startup.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,29 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4747
app.UseHsts();
4848
}
4949
app.UseHttpsRedirection();
50-
app.UseStaticFiles();
50+
51+
app.UseStaticFiles(new StaticFileOptions
52+
{
53+
OnPrepareResponse = context =>
54+
{
55+
var headers = context.Context.Response.Headers;
56+
var contentType = headers["Content-Type"];
57+
if (contentType == "application/x-gzip")
58+
{
59+
if (context.File.Name.EndsWith("js.gz"))
60+
{
61+
contentType = "application/javascript";
62+
}
63+
else if (context.File.Name.EndsWith("css.gz"))
64+
{
65+
contentType = "text/css";
66+
}
67+
68+
headers.Add("Content-Encoding", "gzip");
69+
headers["Content-Type"] = contentType;
70+
}
71+
}
72+
});
5173

5274
app.UseRouting();
5375

0 commit comments

Comments
 (0)