diff --git a/.github/workflows/frontend-build.yml b/.github/workflows/frontend-build.yml
index 7cf969438d..dbd35c0d6e 100644
--- a/.github/workflows/frontend-build.yml
+++ b/.github/workflows/frontend-build.yml
@@ -84,8 +84,9 @@ jobs:
node-version: ${{ inputs.node_version }}
# The E2E shards serve the site with StaticHost, as production does,
- # from the build output this job uploads (statichost-bin). Its restore
- # and compile need no site, so they run alongside the Astro build.
+ # from the build output this job uploads (statichost-bin). StaticHost
+ # reads the site from its web root per request, so its build needs no
+ # site and runs alongside the Astro build.
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
global-json-file: global.json
@@ -138,16 +139,6 @@ jobs:
bun run build:production
fi
- # StaticHost serves its wwwroot, which holds a copy of dist (hard links)
- # as in production. MapStaticAssets lists every file of the web root:
- # this incremental build redoes only that, while the site archive is packed
- # and uploaded.
- - name: Start the StaticHost build
- run: |
- cp -alf dist/. ../statichost/StaticHost/wwwroot/
- "$SETUP" wait statichost-compile
- "$SETUP" start statichost-build dotnet build -c Release --no-restore ../statichost/StaticHost
-
- name: Save Astro build state
if: ${{ success() && (github.event_name == 'push' || steps.astro_cache.outputs.cache-matched-key == '') }}
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
@@ -226,7 +217,7 @@ jobs:
- name: Wait for the StaticHost build
run: |
- "$SETUP" wait statichost-build
+ "$SETUP" wait statichost-compile
"$SETUP" report
- name: Upload the StaticHost build
@@ -457,17 +448,17 @@ jobs:
"$SETUP" start playwright-deps bun run playwright install-deps chromium
"$SETUP" start playwright-browsers bun run playwright install chromium
- # The Frontend Build job built StaticHost against this run's dist.
- # StaticHost reads the web root's files per request, so it starts
- # before the site is there, and boots during its download.
+ # The Frontend Build job built StaticHost. It reads the web root's files
+ # per request, so it starts before the site is there, and boots during
+ # its download.
- name: Download the StaticHost build
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: statichost-bin
path: src/statichost/StaticHost/bin/Release/net10.0
- # Any answer, a 404 included, means StaticHost built its routing table,
- # which the first request does.
+ # Any answer, a 404 included, means StaticHost is up and has built its
+ # routing table, which the first request does.
- name: Start StaticHost
run: |
mkdir -p ../statichost/StaticHost/wwwroot
diff --git a/src/frontend/playwright.config.mjs b/src/frontend/playwright.config.mjs
index 6d1b1e0133..827d21acfc 100644
--- a/src/frontend/playwright.config.mjs
+++ b/src/frontend/playwright.config.mjs
@@ -66,9 +66,9 @@ export default defineConfig({
webServer: isCI
? {
// CI serves the built site with StaticHost, as production does (Markdown negotiation, canonical
- // redirects, the 404 page, headers). The Frontend Build job builds it against the site's files
- // (wwwroot holds a copy of dist), so the tests start its assembly directly: `dotnet run --no-build`
- // still evaluates the project, which takes seconds. The E2E jobs start it before dist arrives
+ // redirects, the 404 page, headers) from its wwwroot, where the E2E jobs unpack dist. The Frontend
+ // Build job builds it, so the tests start its assembly directly: `dotnet run --no-build` still
+ // evaluates the project, which takes seconds. The E2E jobs start it before dist arrives
// (.github/scripts/start-web-server.mjs), hence reuseExistingServer.
command: `dotnet bin/Release/net10.0/StaticHost.dll --urls ${e2eBaseUrl}`,
cwd: fileURLToPath(new URL('../statichost/StaticHost/', import.meta.url)),
diff --git a/src/statichost/StaticHost/AgentReadiness/MarkdownNegotiationMiddleware.cs b/src/statichost/StaticHost/AgentReadiness/MarkdownNegotiationMiddleware.cs
index 8dc52cde71..1c0eed87c4 100644
--- a/src/statichost/StaticHost/AgentReadiness/MarkdownNegotiationMiddleware.cs
+++ b/src/statichost/StaticHost/AgentReadiness/MarkdownNegotiationMiddleware.cs
@@ -14,9 +14,8 @@ namespace StaticHost.AgentReadiness;
/// HTML page (via starlight-page-actions). E.g. /get-started/quickstart/
/// has companion /get-started/quickstart.md. This middleware short-circuits
/// the pipeline by streaming that file directly when markdown is preferred —
-/// it does NOT rewrite Request.Path because rewriting after
-/// UseRouting() would not re-trigger endpoint selection in
-/// MapStaticAssets().
+/// it does NOT rewrite Request.Path, because the negotiated response needs
+/// its own caching headers (below), which the site file middleware would replace.
///
///
/// Therefore the middleware MUST run before UseDefaultFiles and
@@ -44,7 +43,7 @@ public MarkdownNegotiationMiddleware(
{
_next = next;
_logger = logger;
- // Use the same provider MapStaticAssets reads from so wwwroot is the source of truth.
+ // Use the same provider SiteFileMiddleware reads from so wwwroot is the source of truth.
_fileProvider = env.WebRootFileProvider;
}
diff --git a/src/statichost/StaticHost/Caching.targets b/src/statichost/StaticHost/Caching.targets
deleted file mode 100644
index 4d76f64913..0000000000
--- a/src/statichost/StaticHost/Caching.targets
+++ /dev/null
@@ -1,37 +0,0 @@
-
-
-
-
-
- <_AstroEndpoints Include="@(StaticWebAssetEndpoint)"
- Condition="$([System.String]::Copy('%(Identity)').StartsWith('_astro/'))" />
-
-
-
-
- <_CacheHeaderOperation Include="Replace">
- Header
- Cache-Control
- max-age=31536000, public, immutable
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/statichost/StaticHost/GlobalUsings.cs b/src/statichost/StaticHost/GlobalUsings.cs
index 9d5d095639..d7de8cf05c 100644
--- a/src/statichost/StaticHost/GlobalUsings.cs
+++ b/src/statichost/StaticHost/GlobalUsings.cs
@@ -4,6 +4,7 @@
global using StaticHost.AgentReadiness;
global using StaticHost.Live;
global using StaticHost.Routing;
+global using StaticHost.SiteFiles;
global using StaticHost.Telemetry;
global using OpenTelemetry;
diff --git a/src/statichost/StaticHost/Program.cs b/src/statichost/StaticHost/Program.cs
index c76c08fd58..1a26619474 100644
--- a/src/statichost/StaticHost/Program.cs
+++ b/src/statichost/StaticHost/Program.cs
@@ -16,6 +16,7 @@
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore);
builder.AddLiveStatus();
+builder.Services.AddSiteFiles();
builder.Services.AddHsts(options =>
{
@@ -34,6 +35,9 @@
// Restore the Front Door client IP before telemetry or any middleware reads it.
app.UseForwardedClientIp();
+// Before anything that redirects: Front Door must never cache a redirect.
+app.UseNoStoreRedirects();
+
// Only enable HSTS in production
if (!app.Environment.IsDevelopment())
{
@@ -47,8 +51,7 @@
// Agent-readiness middlewares MUST run before UseDefaultFiles + UseRouting:
// * UseDefaultFiles rewrites /foo/ -> /foo/index.html, breaking .md-companion mapping.
-// * MapStaticAssets registers endpoints during UseRouting, so a path rewrite
-// after UseRouting wouldn't re-trigger endpoint selection.
+// * A path rewrite after UseRouting wouldn't re-trigger endpoint selection.
// MarkdownNegotiationMiddleware short-circuits to serve the .md body directly;
// LinkHeaderMiddleware attaches a Link header on HTML 2xx responses via OnStarting.
app.UseAgentReadiness();
@@ -96,6 +99,10 @@
await next();
});
+// The site's files. Requests that matched an endpoint below (health, downloads, live status)
+// pass through to it; a file that does not exist falls through to the 404 page above.
+app.UseSiteFiles();
+
app.MapGet("/healthz", (HttpContext context) =>
{
// A health probe must always reach the origin, never a cached copy.
@@ -143,6 +150,4 @@
return Results.Redirect("https://aka.ms/aspire/get/install.sh");
});
-app.MapStaticAssets();
-
await app.RunAsync();
diff --git a/src/statichost/StaticHost/Routing/CanonicalPathRedirectExtensions.cs b/src/statichost/StaticHost/Routing/CanonicalPathRedirectExtensions.cs
index 8edacc3d0e..709ffcc5d3 100644
--- a/src/statichost/StaticHost/Routing/CanonicalPathRedirectExtensions.cs
+++ b/src/statichost/StaticHost/Routing/CanonicalPathRedirectExtensions.cs
@@ -13,4 +13,16 @@ public static IApplicationBuilder UseCanonicalPathRedirects(this IApplicationBui
app.UseMiddleware();
return app;
}
+
+ ///
+ /// Sends Cache-Control: no-store with every redirect, so no cache keeps one.
+ /// Call first, before any middleware or endpoint that can redirect.
+ ///
+ public static IApplicationBuilder UseNoStoreRedirects(this IApplicationBuilder app)
+ {
+ ArgumentNullException.ThrowIfNull(app);
+
+ app.UseMiddleware();
+ return app;
+ }
}
diff --git a/src/statichost/StaticHost/Routing/NoStoreRedirectMiddleware.cs b/src/statichost/StaticHost/Routing/NoStoreRedirectMiddleware.cs
new file mode 100644
index 0000000000..a8a62a3461
--- /dev/null
+++ b/src/statichost/StaticHost/Routing/NoStoreRedirectMiddleware.cs
@@ -0,0 +1,37 @@
+namespace StaticHost.Routing;
+
+///
+/// Marks every redirect the host sends Cache-Control: no-store.
+///
+///
+/// A redirect without Cache-Control is heuristically cacheable, and a permanent one is
+/// cacheable by default (RFC 9111, 4.2.2). Azure Front Door would keep such a redirect for days,
+/// so a canonical path, an HTTPS upgrade or an install script target that changes would go on
+/// redirecting from the edge. Run it first, so it sees the redirects of every later middleware
+/// and endpoint. 304 Not Modified is not a redirect and keeps the file's caching.
+///
+internal sealed class NoStoreRedirectMiddleware(RequestDelegate next)
+{
+ public Task InvokeAsync(HttpContext context)
+ {
+ context.Response.OnStarting(static state =>
+ {
+ var response = (HttpResponse)state;
+ if (IsRedirect(response.StatusCode))
+ {
+ response.Headers.CacheControl = "no-store";
+ }
+
+ return Task.CompletedTask;
+ }, context.Response);
+
+ return next(context);
+ }
+
+ internal static bool IsRedirect(int statusCode) => statusCode
+ is StatusCodes.Status301MovedPermanently
+ or StatusCodes.Status302Found
+ or StatusCodes.Status303SeeOther
+ or StatusCodes.Status307TemporaryRedirect
+ or StatusCodes.Status308PermanentRedirect;
+}
diff --git a/src/statichost/StaticHost/Site.targets b/src/statichost/StaticHost/Site.targets
new file mode 100644
index 0000000000..785b8eb872
--- /dev/null
+++ b/src/statichost/StaticHost/Site.targets
@@ -0,0 +1,57 @@
+
+
+
+
+ .html;.htm;.md;.markdown;.txt;.xml;.json;.webmanifest;.js;.mjs;.jsx;.css;.map;.svg;.ico;.bmp;.otf;.wasm;.csv;.tsv;.ics;.xsl;.xslt
+ $(MSBuildThisFileDirectory)../compress-site.cs
+
+ $(DOTNET_HOST_PATH)
+ dotnet
+
+
+
+
+
+ <_SiteIntermediateDirectory>$(MSBuildProjectDirectory)/$(IntermediateOutputPath)
+
+ <_SiteCompressedDirectory>$(_SiteIntermediateDirectory)site-br-q11/
+ <_SiteFileList>$(_SiteIntermediateDirectory)site-files.txt
+ <_SiteBrotliList>$(_SiteIntermediateDirectory)site-br.txt
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/statichost/StaticHost/SiteFiles/SiteFileCacheOptions.cs b/src/statichost/StaticHost/SiteFiles/SiteFileCacheOptions.cs
new file mode 100644
index 0000000000..95fe060d13
--- /dev/null
+++ b/src/statichost/StaticHost/SiteFiles/SiteFileCacheOptions.cs
@@ -0,0 +1,59 @@
+namespace StaticHost.SiteFiles;
+
+///
+/// The Cache-Control values sends, bound from the
+/// SiteFiles:CacheControl configuration section so they can be tuned with App Service
+/// settings (for example SiteFiles__CacheControl__Default) and a restart, without a deploy.
+///
+///
+///
+/// Azure Front Door caches only fingerprinted content, and deployments don't purge it. A file
+/// that isn't fingerprinted keeps its URL when its content changes, so it defaults to
+/// no-cache: browsers revalidate it on every use, and Front Door never caches a response
+/// the origin marks no-cache, even on a route or rule that enables caching.
+///
+///
+internal sealed class SiteFileCacheOptions
+{
+ public const string SectionName = "SiteFiles:CacheControl";
+
+ // A file that isn't fingerprinted can change without a new URL, so nothing may reuse it unchecked.
+ internal const string DefaultValue = "no-cache";
+
+ // The SDK's default for images, video and fonts, as MapStaticAssets sent.
+ internal const string MediaValue = "max-age=3600, must-revalidate";
+
+ // https://starlight.astro.build/environmental-impact/#caching
+ internal const string ImmutableValue = "max-age=31536000, public, immutable";
+
+ /// The value for files that are neither media nor immutable: HTML, markdown, JSON, XML, text.
+ public string Default { get; set; } = DefaultValue;
+
+ /// The value for images, video and fonts outside the immutable paths.
+ public string Media { get; set; } = MediaValue;
+
+ /// The value for fingerprinted files, and for files requested with the version query parameter.
+ public string Immutable { get; set; } = ImmutableValue;
+
+ ///
+ /// Paths whose files are fingerprinted, so they can be cached forever, separated by semicolons.
+ /// Astro hashes the names of the files it emits under /_astro/. A string rather than a
+ /// list, because configuration adds to a list's default instead of replacing it.
+ ///
+ public string ImmutablePathPrefixes { get; set; } = "/_astro";
+
+ ///
+ /// The query parameter that marks a URL as versioned (sidebar.json?v=<hash>), so its
+ /// response can be cached forever: a new version gets a new URL. Empty turns it off.
+ ///
+ public string VersionQueryParameter { get; set; } = "v";
+
+ internal string[] GetImmutablePathPrefixes() =>
+ ImmutablePathPrefixes.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
+
+ internal static bool IsValid(SiteFileCacheOptions options) =>
+ !string.IsNullOrWhiteSpace(options.Default)
+ && !string.IsNullOrWhiteSpace(options.Media)
+ && !string.IsNullOrWhiteSpace(options.Immutable)
+ && options.GetImmutablePathPrefixes().All(static prefix => prefix is ['/', _, ..] && !prefix.EndsWith('/'));
+}
diff --git a/src/statichost/StaticHost/SiteFiles/SiteFileETags.cs b/src/statichost/StaticHost/SiteFiles/SiteFileETags.cs
new file mode 100644
index 0000000000..195e11d2c5
--- /dev/null
+++ b/src/statichost/StaticHost/SiteFiles/SiteFileETags.cs
@@ -0,0 +1,91 @@
+using System.Buffers;
+using System.Collections.Concurrent;
+using System.IO.Hashing;
+using Microsoft.Extensions.FileProviders;
+
+namespace StaticHost.SiteFiles;
+
+///
+/// Computes a file's ETag from a hash of its content the first time it is served, then keeps it
+/// in memory.
+///
+///
+///
+/// The hash is XxHash128: an ETag only has to tell versions of a file apart, which needs no
+/// cryptographic hash. A content hash, unlike the length and last-modified time
+/// UseStaticFiles uses, stays the same across deployments for files that did not change,
+/// though CI writes every file again. An entry is recomputed when the file's length or
+/// last-modified time changes, which covers a web root that is replaced while StaticHost runs
+/// (CI unpacks the site after StaticHost starts).
+///
+///
+/// Length and time do not identify the bytes, though: a restore can replace a file with one of
+/// the same length and time. A cached ETag is therefore only used where a stale one is harmless,
+/// on a response that sends the file. A request with a precondition, which a stale ETag could
+/// answer with a 304 for bytes the client does not have, hashes the file again
+/// (revalidate). Watching the web root for changes would cost an inotify watch per
+/// directory on Linux, and .NET does not expose the inode or change time that would tell a
+/// replaced file apart.
+///
+///
+internal sealed class SiteFileETags
+{
+ private const int BufferSize = 64 * 1024;
+
+ // Keyed by the file's physical path, which the provider normalizes: the request paths
+ // /a/b.json, /a//b.json and /a///b.json are one file, so they share one entry, and the
+ // dictionary holds at most one entry per file of the web root. Request paths would let any
+ // client add entries without bound, each one costing a hash of the file.
+ // Ordinal, as the file system is in production (Linux): two paths that differ only by case are
+ // two files there, and must not share an entry. On a case-insensitive disk they are one file
+ // under two keys, which costs one more hash. Canonical redirects send most requests to one casing.
+ private readonly ConcurrentDictionary _entries = new(StringComparer.Ordinal);
+
+ internal int Count => _entries.Count;
+
+ ///
+ /// The file's path in the web root, the key when the provider gives no physical path.
+ ///
+ /// The file to hash.
+ /// Hash the file even when an entry matches its length and time.
+ /// Cancels the hashing.
+ public async ValueTask GetAsync(string path, IFileInfo file, bool revalidate, CancellationToken cancellationToken)
+ {
+ var key = file.PhysicalPath ?? path;
+ var length = file.Length;
+ var lastModified = file.LastModified;
+
+ if (!revalidate
+ && _entries.TryGetValue(key, out var entry)
+ && entry.Length == length
+ && entry.LastModified == lastModified)
+ {
+ return entry.ETag;
+ }
+
+ // Two first requests for the same file may both hash it; the result is the same. That is
+ // not worth coordinating: it happens once per file per instance, and sending the file
+ // reads it anyway.
+ var hash = new XxHash128();
+ var buffer = ArrayPool.Shared.Rent(BufferSize);
+ try
+ {
+ await using var stream = file.CreateReadStream();
+ int read;
+ while ((read = await stream.ReadAsync(buffer, cancellationToken)) > 0)
+ {
+ hash.Append(buffer.AsSpan(0, read));
+ }
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(buffer);
+ }
+
+ var etag = $"\"{Convert.ToBase64String(hash.GetCurrentHash())}\"";
+ _entries[key] = new Entry(length, lastModified, etag);
+ return etag;
+ }
+
+ private sealed record Entry(long Length, DateTimeOffset LastModified, string ETag);
+}
diff --git a/src/statichost/StaticHost/SiteFiles/SiteFileExtensions.cs b/src/statichost/StaticHost/SiteFiles/SiteFileExtensions.cs
new file mode 100644
index 0000000000..f8111116a7
--- /dev/null
+++ b/src/statichost/StaticHost/SiteFiles/SiteFileExtensions.cs
@@ -0,0 +1,23 @@
+namespace StaticHost.SiteFiles;
+
+internal static class SiteFileExtensions
+{
+ public static IServiceCollection AddSiteFiles(this IServiceCollection services)
+ {
+ services.AddOptions()
+ .BindConfiguration(SiteFileCacheOptions.SectionName)
+ .Validate(
+ SiteFileCacheOptions.IsValid,
+ $"{SiteFileCacheOptions.SectionName} needs non-empty values, and path prefixes that start with '/' and do not end with it.")
+ .ValidateOnStart();
+
+ return services.AddSingleton();
+ }
+
+ ///
+ /// Serves the web root's files, preferring the precompressed brotli copy. Call it after
+ /// UseRouting: it leaves requests that matched an endpoint to that endpoint.
+ ///
+ public static IApplicationBuilder UseSiteFiles(this IApplicationBuilder app) =>
+ app.UseMiddleware();
+}
diff --git a/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs b/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs
new file mode 100644
index 0000000000..b62a6bd2cf
--- /dev/null
+++ b/src/statichost/StaticHost/SiteFiles/SiteFileMiddleware.cs
@@ -0,0 +1,350 @@
+using Microsoft.AspNetCore.Http.Headers;
+using Microsoft.AspNetCore.StaticFiles;
+using Microsoft.Extensions.FileProviders;
+using Microsoft.Extensions.Options;
+using Microsoft.Extensions.Primitives;
+using Microsoft.Net.Http.Headers;
+
+namespace StaticHost.SiteFiles;
+
+///
+/// Serves the site's files from the web root, with the brotli copy the publish wrote next to a
+/// file (page.html.br) when the client accepts it.
+///
+///
+///
+/// This replaces MapStaticAssets, which reads a manifest of every file at startup and builds
+/// a route for each one. With about 70,000 files, that manifest took a minute of every build and
+/// publish, and each instance seconds and gigabytes of memory to start. This middleware keeps no
+/// per-file state up front: it looks the file up on each request, as UseStaticFiles does,
+/// and computes the ETag the first time a file is served ().
+///
+///
+/// It runs after UseRouting and steps aside when an endpoint matched, so the API, health
+/// and download endpoints keep precedence over a file of the same path, as they had over the
+/// static asset endpoints.
+///
+///
+/// A request for one byte range gets it (video players need ranges); other range requests get the
+/// whole file. A range request always gets the file itself, never its brotli copy: a range of
+/// compressed bytes is useless to a client that wants to seek, and Azure Front Door answers 503
+/// when the lengths it sees for one URL's ranges disagree across encodings.
+///
+///
+/// Cache-Control comes from .
+///
+///
+internal sealed class SiteFileMiddleware
+{
+ private const string BrotliExtension = ".br";
+
+ private const string UnknownContentType = "application/octet-stream";
+
+ private const string AllowedMethods = "GET, HEAD";
+
+ private readonly RequestDelegate _next;
+ private readonly IFileProvider _fileProvider;
+ private readonly SiteFileETags _etags;
+ private readonly SiteFileCacheOptions _cacheOptions;
+ private readonly PathString[] _immutablePrefixes;
+ private readonly FileExtensionContentTypeProvider _contentTypes;
+
+ public SiteFileMiddleware(
+ RequestDelegate next, IWebHostEnvironment environment, SiteFileETags etags, IOptions cacheOptions)
+ {
+ _next = next;
+ _fileProvider = environment.WebRootFileProvider;
+ _etags = etags;
+ _cacheOptions = cacheOptions.Value;
+ _immutablePrefixes = [.. _cacheOptions.GetImmutablePathPrefixes().Select(static prefix => new PathString(prefix))];
+ _contentTypes = CreateContentTypeProvider();
+ }
+
+ public async Task InvokeAsync(HttpContext context)
+ {
+ var request = context.Request;
+ if (context.GetEndpoint() is not null
+ || !request.Path.HasValue
+ || request.Path.Value.EndsWith('/'))
+ {
+ await _next(context);
+ return;
+ }
+
+ var file = _fileProvider.GetFileInfo(request.Path.Value);
+ if (!file.Exists || file.IsDirectory)
+ {
+ await _next(context);
+ return;
+ }
+
+ // A file only answers GET and HEAD. Other methods get 405, as they did from the static
+ // asset endpoints, which routing matched by path and then rejected by method.
+ if (!HttpMethods.IsGet(request.Method) && !HttpMethods.IsHead(request.Method))
+ {
+ context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
+ context.Response.Headers.Allow = AllowedMethods;
+ return;
+ }
+
+ // Every file the site emits is meant to be served, including types this provider does
+ // not know (Pagefind's index, asciinema casts), as MapStaticAssets did.
+ if (!_contentTypes.TryGetContentType(request.Path.Value, out var contentType))
+ {
+ contentType = UnknownContentType;
+ }
+
+ // Look for the brotli copy even when the client does not accept brotli: a file that has one
+ // must send Vary: Accept-Encoding with both versions, so that a cache does not hand the
+ // uncompressed one to every client. Nearly every browser accepts brotli anyway.
+ var compressedPath = request.Path.Value + BrotliExtension;
+ var compressed = _fileProvider.GetFileInfo(compressedPath);
+ var hasCompressed = compressed.Exists && !compressed.IsDirectory;
+ var useCompressed = hasCompressed
+ && request.Headers.Range.Count == 0
+ && AcceptsBrotli(request.Headers.AcceptEncoding);
+ var served = useCompressed ? compressed : file;
+
+ var response = context.Response;
+ // A precondition decides on the ETag, so it must match the bytes on disk now: a 304 for a
+ // file replaced with one of the same length and time would keep a client on the old bytes.
+ var revalidate = request.Headers.IfMatch.Count > 0
+ || request.Headers.IfNoneMatch.Count > 0
+ || request.Headers.IfRange.Count > 0;
+ var etag = await _etags.GetAsync(useCompressed ? compressedPath : request.Path.Value, served, revalidate, context.RequestAborted);
+ var entityTag = new EntityTagHeaderValue(etag);
+ var lastModified = TruncateToSeconds(file.LastModified);
+ var requestHeaders = request.GetTypedHeaders();
+
+ var headers = response.Headers;
+ headers.CacheControl = GetCacheControl(request, contentType);
+ headers.ETag = etag;
+ headers.LastModified = HeaderUtilities.FormatDate(lastModified);
+ if (hasCompressed)
+ {
+ headers.Vary = HeaderNames.AcceptEncoding;
+ }
+
+ if (EvaluatePreconditions(requestHeaders, entityTag, lastModified) is { } status)
+ {
+ response.StatusCode = status;
+ return;
+ }
+
+ headers.AcceptRanges = "bytes";
+ // Range applies only to GET (RFC 9110, 14.2): HEAD gets the headers of the whole file.
+ var unsatisfiable = false;
+ var range = HttpMethods.IsGet(request.Method)
+ ? GetRange(requestHeaders, entityTag, lastModified, served.Length, out unsatisfiable)
+ : null;
+ if (unsatisfiable)
+ {
+ response.StatusCode = StatusCodes.Status416RangeNotSatisfiable;
+ headers.ContentRange = $"bytes */{served.Length}";
+ return;
+ }
+
+ response.ContentType = contentType;
+ if (useCompressed)
+ {
+ headers.ContentEncoding = "br";
+ }
+
+ var (offset, count) = range ?? (0, served.Length);
+ if (range is not null)
+ {
+ response.StatusCode = StatusCodes.Status206PartialContent;
+ headers.ContentRange = $"bytes {offset}-{offset + count - 1}/{served.Length}";
+ }
+ else
+ {
+ response.StatusCode = StatusCodes.Status200OK;
+ }
+
+ response.ContentLength = count;
+ if (HttpMethods.IsHead(request.Method))
+ {
+ return;
+ }
+
+ await response.SendFileAsync(served, offset, count, context.RequestAborted);
+ }
+
+ internal string GetCacheControl(HttpRequest request, string contentType)
+ {
+ var path = request.Path;
+ foreach (var prefix in _immutablePrefixes)
+ {
+ if (path.StartsWithSegments(prefix, StringComparison.Ordinal))
+ {
+ return _cacheOptions.Immutable;
+ }
+ }
+
+ // A versioned URL changes with its content, so its response never goes stale.
+ if (_cacheOptions.VersionQueryParameter is { Length: > 0 } version
+ && !StringValues.IsNullOrEmpty(request.Query[version]))
+ {
+ return _cacheOptions.Immutable;
+ }
+
+ return contentType.StartsWith("image/", StringComparison.Ordinal)
+ || contentType.StartsWith("video/", StringComparison.Ordinal)
+ || contentType.StartsWith("font/", StringComparison.Ordinal)
+ ? _cacheOptions.Media
+ : _cacheOptions.Default;
+ }
+
+ ///
+ /// Chooses between the brotli copy and the file itself by the quality values of
+ /// Accept-Encoding (RFC 9110, 12.5.3), and returns whether to send brotli.
+ ///
+ ///
+ /// * applies to each coding the header does not list, identity included. Unlisted,
+ /// identity stays acceptable but preferred last, so br;q=0.5 still gets brotli, and a tie
+ /// goes to brotli. When the client accepts neither, it gets the file itself: RFC 9110, 12.1 lets
+ /// the server disregard the header, and a 406 could be cached for clients that accept the file.
+ ///
+ internal static bool AcceptsBrotli(StringValues acceptEncoding)
+ {
+ if (!StringWithQualityHeaderValue.TryParseList(acceptEncoding, out var encodings))
+ {
+ return false;
+ }
+
+ double? brotli = null;
+ double? identity = null;
+ double? wildcard = null;
+ foreach (var encoding in encodings)
+ {
+ var quality = encoding.Quality ?? 1;
+ if (StringSegment.Equals(encoding.Value, "br", StringComparison.OrdinalIgnoreCase))
+ {
+ brotli = quality;
+ }
+ else if (StringSegment.Equals(encoding.Value, "identity", StringComparison.OrdinalIgnoreCase))
+ {
+ identity = quality;
+ }
+ else if (StringSegment.Equals(encoding.Value, "*", StringComparison.Ordinal))
+ {
+ wildcard = quality;
+ }
+ }
+
+ // An explicit "br;q=0" refuses brotli even when "*" accepts everything else.
+ var brotliQuality = brotli ?? wildcard ?? 0;
+ return brotliQuality > 0 && brotliQuality >= (identity ?? wildcard ?? 0);
+ }
+
+ ///
+ /// Evaluates the request's preconditions in the order of RFC 9110, 13.2.2, as
+ /// MapStaticAssets did. Returns 412 or 304 when one decides the response, otherwise
+ /// .
+ ///
+ private static int? EvaluatePreconditions(RequestHeaders requestHeaders, EntityTagHeaderValue etag, DateTimeOffset lastModified)
+ {
+ // If-Match takes precedence over If-Unmodified-Since, and uses the strong comparison.
+ var ifMatch = requestHeaders.IfMatch;
+ if (ifMatch.Count > 0)
+ {
+ if (!Matches(ifMatch, etag, useStrongComparison: true))
+ {
+ return StatusCodes.Status412PreconditionFailed;
+ }
+ }
+ else if (requestHeaders.IfUnmodifiedSince is { } ifUnmodifiedSince && lastModified > ifUnmodifiedSince)
+ {
+ return StatusCodes.Status412PreconditionFailed;
+ }
+
+ // If-None-Match takes precedence over If-Modified-Since (RFC 9110, 13.1.3).
+ var ifNoneMatch = requestHeaders.IfNoneMatch;
+ if (ifNoneMatch.Count > 0)
+ {
+ return Matches(ifNoneMatch, etag, useStrongComparison: false) ? StatusCodes.Status304NotModified : null;
+ }
+
+ return requestHeaders.IfModifiedSince is { } ifModifiedSince && lastModified <= ifModifiedSince
+ ? StatusCodes.Status304NotModified
+ : null;
+ }
+
+ private static bool Matches(IList candidates, EntityTagHeaderValue etag, bool useStrongComparison)
+ {
+ foreach (var candidate in candidates)
+ {
+ if (candidate.Equals(EntityTagHeaderValue.Any) || candidate.Compare(etag, useStrongComparison))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Returns the single byte range to send, or to send the whole file,
+ /// which a server may do for any range request it does not support, such as several ranges.
+ /// Safari only plays video from servers that answer ranges.
+ ///
+ private static (long Offset, long Count)? GetRange(
+ RequestHeaders requestHeaders, EntityTagHeaderValue etag, DateTimeOffset lastModified, long length, out bool unsatisfiable)
+ {
+ unsatisfiable = false;
+ var range = requestHeaders.Range;
+ if (range is null
+ || !StringSegment.Equals(range.Unit, "bytes", StringComparison.OrdinalIgnoreCase)
+ || range.Ranges.Count != 1)
+ {
+ return null;
+ }
+
+ // If-Range applies the range only to the representation the client has (RFC 9110, 13.1.5).
+ if (requestHeaders.IfRange is { } ifRange)
+ {
+ var matches = ifRange.EntityTag is { } tag
+ ? tag.Compare(etag, useStrongComparison: true)
+ : ifRange.LastModified == lastModified;
+ if (!matches)
+ {
+ return null;
+ }
+ }
+
+ var item = range.Ranges.First();
+ long start, end;
+ if (item.From is { } from)
+ {
+ start = from;
+ end = Math.Min(item.To ?? long.MaxValue, length - 1);
+ }
+ else
+ {
+ // A suffix range: the last N bytes.
+ start = Math.Max(0, length - (item.To ?? 0));
+ end = length - 1;
+ }
+
+ if (start >= length || start > end)
+ {
+ unsatisfiable = true;
+ return null;
+ }
+
+ return (start, end - start + 1);
+ }
+
+ private static DateTimeOffset TruncateToSeconds(DateTimeOffset value) =>
+ new(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Offset);
+
+ private static FileExtensionContentTypeProvider CreateContentTypeProvider()
+ {
+ var provider = new FileExtensionContentTypeProvider();
+
+ // The SDK's static web assets mapping, which production sent until now, differs here.
+ provider.Mappings[".md"] = "text/markdown";
+
+ return provider;
+ }
+}
diff --git a/src/statichost/StaticHost/StaticHost.csproj b/src/statichost/StaticHost/StaticHost.csproj
index a836de6f0c..791917b5e4 100644
--- a/src/statichost/StaticHost/StaticHost.csproj
+++ b/src/statichost/StaticHost/StaticHost.csproj
@@ -8,11 +8,14 @@
true
noble-chiseled
-
- false
- brotli
-
- <_BlazorBrotliCompressionLevel>Optimal
+
+ false
+
+ $(DefaultItemExcludes);wwwroot/**
@@ -27,6 +30,7 @@
+
@@ -50,5 +54,5 @@
-
+
diff --git a/src/statichost/compress-site.cs b/src/statichost/compress-site.cs
new file mode 100644
index 0000000000..8a228aa38f
--- /dev/null
+++ b/src/statichost/compress-site.cs
@@ -0,0 +1,84 @@
+// Writes a brotli copy of each text file of the site, for StaticHost to serve to clients that
+// accept it. StaticHost's publish runs it (StaticHost/Site.targets); it also runs alone:
+//
+// dotnet run --file compress-site.cs --