Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tests/StaticHost.Tests/SiteFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading