Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MahdiKarimipour/53813ac00a4f9ebc020c6b94fa063791 to your computer and use it in GitHub Desktop.
Save MahdiKarimipour/53813ac00a4f9ebc020c6b94fa063791 to your computer and use it in GitHub Desktop.
Caching and Compression in Asp.NET for React
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
});
services.AddResponseCaching();
}
//Middleware
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
app.UseResponseCaching();
app.UseStaticFiles(new StaticFileOptions()
{
HttpsCompression = Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Compress,
OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(1)
};
}
});
app.UseSpaStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "UI/build")),
RequestPath = "/Static",
HttpsCompression = Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Compress,
OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(1)
};
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment