Skip to content

Commit

Permalink
configure static files middleware to set Content-Type, Content-Encodi…
Browse files Browse the repository at this point in the history
…ng repsonse headers for gzipped resources accordingly
  • Loading branch information
kajan committed Feb 18, 2021
1 parent 140436b commit 8edea81
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion AspNetCoreVueMpa.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,29 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
var headers = context.Context.Response.Headers;
var contentType = headers["Content-Type"];
if (contentType == "application/x-gzip")
{
if (context.File.Name.EndsWith("js.gz"))
{
contentType = "application/javascript";
}
else if (context.File.Name.EndsWith("css.gz"))
{
contentType = "text/css";
}

headers.Add("Content-Encoding", "gzip");
headers["Content-Type"] = contentType;
}
}
});

app.UseRouting();

Expand Down

0 comments on commit 8edea81

Please sign in to comment.