From b2fa3d995ca8eaebb110e6a985ba07909d374ec7 Mon Sep 17 00:00:00 2001 From: David Pine <7679720+IEvangelist@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:42:37 -0600 Subject: [PATCH] Cache versioned JSON as immutable sidebar.json and the locale fallback dictionaries are requested with a content hash in ?v=, so a new version gets a new URL. Serve them with the immutable Cache-Control so browsers stop revalidating them and Front Door can cache them. Limited to JSON so a page link that happens to carry ?v= is never cached as immutable. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../StaticHost/SiteFiles/SiteFileMiddleware.cs | 15 +++++++++++++-- tests/StaticHost.Tests/SiteFileTests.cs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs b/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs index 844c6b98e..68da4c9f9 100644 --- a/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs +++ b/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs @@ -47,6 +47,8 @@ internal sealed class SiteFileMiddleware private static readonly PathString s_immutablePrefix = "/_astro"; + private const string VersionQueryParameter = "v"; + private readonly RequestDelegate _next; private readonly IFileProvider _fileProvider; private readonly SiteFileETags _etags; @@ -115,7 +117,7 @@ public async Task InvokeAsync(HttpContext context) var requestHeaders = request.GetTypedHeaders(); var headers = response.Headers; - headers.CacheControl = GetCacheControl(request.Path, contentType); + headers.CacheControl = GetCacheControl(request.Path, request.Query, contentType); headers.ETag = etag; headers.LastModified = HeaderUtilities.FormatDate(lastModified); if (hasCompressed) @@ -170,13 +172,22 @@ public async Task InvokeAsync(HttpContext context) await response.SendFileAsync(served, offset, count, context.RequestAborted); } - internal static string GetCacheControl(PathString path, string contentType) + internal static string GetCacheControl(PathString path, IQueryCollection query, string contentType) { if (path.StartsWithSegments(s_immutablePrefix, StringComparison.Ordinal)) { return ImmutableCacheControl; } + // The site requests its JSON data (sidebar.json, locale fallback dictionaries) with a + // content hash in ?v=, so a new version gets a new URL. Limited to JSON so a page link + // that happens to carry ?v= is never cached as immutable. + if (contentType.StartsWith("application/json", StringComparison.Ordinal) + && !string.IsNullOrEmpty(query[VersionQueryParameter])) + { + return ImmutableCacheControl; + } + return contentType.StartsWith("image/", StringComparison.Ordinal) || contentType.StartsWith("video/", StringComparison.Ordinal) || contentType.StartsWith("font/", StringComparison.Ordinal) diff --git a/tests/StaticHost.Tests/SiteFileTests.cs b/tests/StaticHost.Tests/SiteFileTests.cs index 0c3ada8d9..5d399afad 100644 --- a/tests/StaticHost.Tests/SiteFileTests.cs +++ b/tests/StaticHost.Tests/SiteFileTests.cs @@ -270,6 +270,24 @@ public async Task Cache_control_depends_on_the_path_and_the_type(string path, st Assert.Equal(expected, response.Headers.NonValidated["Cache-Control"].ToString()); } + [Theory] + [InlineData("/reference/api/csharp/pkg/sidebar.json?v=abc123", SiteFileMiddleware.ImmutableCacheControl)] + [InlineData("/locale-fallback/de.json?v=0123456789ab", SiteFileMiddleware.ImmutableCacheControl)] + [InlineData("/reference/api/csharp/pkg/sidebar.json", SiteFileMiddleware.DefaultCacheControl)] + [InlineData("/reference/api/csharp/pkg/sidebar.json?v=", SiteFileMiddleware.DefaultCacheControl)] + [InlineData("/reference/api/csharp/pkg/sidebar.json?version=abc123", SiteFileMiddleware.DefaultCacheControl)] + [InlineData("/docs/page/index.html?v=abc123", SiteFileMiddleware.DefaultCacheControl)] + public async Task Versioned_json_is_immutable(string url, string expected) + { + var path = url.Split('?')[0]; + await using var server = await AgentReadinessTestServer.StartAsync(root => root.WriteFile(path, "{}")); + + using var response = await server.Client.GetAsync(url); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expected, response.Headers.NonValidated["Cache-Control"].ToString()); + } + [Fact] public async Task Markdown_is_served_as_text_markdown() {