Created
October 29, 2021 00:04
-
-
Save MahdiKarimipour/53813ac00a4f9ebc020c6b94fa063791 to your computer and use it in GitHub Desktop.
Caching and Compression in Asp.NET for React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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