From 2ad620e8b81b5d3eb1320586ef452adc2016fe55 Mon Sep 17 00:00:00 2001 From: Shilpi Goel Date: Fri, 13 Dec 2024 14:20:45 +0000 Subject: [PATCH 1/2] added cache control --- .../CacheControlMiddleware.cs | 25 +++++++++++++++++++ Frontend/CO.CDP.OrganisationApp/Program.cs | 1 + 2 files changed, 26 insertions(+) create mode 100644 Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs diff --git a/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs b/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs new file mode 100644 index 000000000..fd753ac54 --- /dev/null +++ b/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Http; + +public class CacheControlMiddleware +{ + private readonly RequestDelegate _next; + + public CacheControlMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task Invoke(HttpContext context) + { + context.Response.OnStarting(() => + { + if (!context.Response.Headers.ContainsKey("Cache-Control")) + { + context.Response.Headers["Cache-Control"] = "private, no-store, no-cache, must-revalidate"; + } + return Task.CompletedTask; + }); + + await _next(context); + } +} \ No newline at end of file diff --git a/Frontend/CO.CDP.OrganisationApp/Program.cs b/Frontend/CO.CDP.OrganisationApp/Program.cs index 9e541fb4d..bca9b3c83 100644 --- a/Frontend/CO.CDP.OrganisationApp/Program.cs +++ b/Frontend/CO.CDP.OrganisationApp/Program.cs @@ -241,6 +241,7 @@ app.UseForwardedHeaders(); app.UseMiddleware(); app.UseMiddleware(); +app.UseMiddleware(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) From 9b1b0f3b96e9f73abb2b2c2a2b6a8d6f56e0b1e0 Mon Sep 17 00:00:00 2001 From: Shilpi Goel Date: Mon, 16 Dec 2024 11:12:19 +0000 Subject: [PATCH 2/2] Exculded static files to add in cache-control header --- .../CacheControlMiddleware.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs b/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs index fd753ac54..845305dc5 100644 --- a/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs +++ b/Frontend/CO.CDP.OrganisationApp/CacheControlMiddleware.cs @@ -1,16 +1,26 @@ using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.StaticFiles; public class CacheControlMiddleware { private readonly RequestDelegate _next; + private readonly IContentTypeProvider _contentTypeProvider; + public CacheControlMiddleware(RequestDelegate next) { _next = next; + _contentTypeProvider = new FileExtensionContentTypeProvider(); + } public async Task Invoke(HttpContext context) { + if (IsStaticFileRequest(context.Request.Path)) + { + await _next(context); + return; + } context.Response.OnStarting(() => { if (!context.Response.Headers.ContainsKey("Cache-Control")) @@ -22,4 +32,10 @@ public async Task Invoke(HttpContext context) await _next(context); } + + private bool IsStaticFileRequest(PathString path) + { + var fileExtension = System.IO.Path.GetExtension(path); + return fileExtension != null && _contentTypeProvider.TryGetContentType(fileExtension, out _); + } } \ No newline at end of file