Skip to content

Commit

Permalink
Merge pull request #261 from ucdavis/srk/cache-bust
Browse files Browse the repository at this point in the history
never cache anything outside the API
  • Loading branch information
srkirkland authored Jan 10, 2024
2 parents dd47732 + 1543127 commit a83df4f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Finjector.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.EntityFrameworkCore;
using Finjector.Web.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.StaticFiles.Infrastructure;

#if DEBUG
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
Expand Down Expand Up @@ -138,6 +139,29 @@
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

// we want to disable caching for all html responses outside of the API
app.Use(async (context, next) =>
{
context.Response.OnStarting(() =>
{
if (context.Request.Path.StartsWithSegments("/api") == false &&
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
context.Response.ContentType != null &&
context.Response.ContentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase))
{
// Set the necessary headers to disable caching
context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] =
"no-store, no-cache, must-revalidate";
context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Pragma] = "no-cache";
context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Expires] = "0";
}

return Task.CompletedTask;
});

await next();
});

app.UseHttpsRedirection();
app.UseStaticFiles();
Expand Down

0 comments on commit a83df4f

Please sign in to comment.