Skip to content
Draft
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
27 changes: 9 additions & 18 deletions .github/workflows/frontend-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -218,7 +209,7 @@ jobs:

- name: Wait for the StaticHost build
run: |
"$SETUP" wait statichost-build
"$SETUP" wait statichost-compile
"$SETUP" report

- name: Upload the StaticHost build
Expand Down Expand Up @@ -449,17 +440,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
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/playwright.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ namespace StaticHost.AgentReadiness;
/// HTML page (via <c>starlight-page-actions</c>). E.g. <c>/get-started/quickstart/</c>
/// has companion <c>/get-started/quickstart.md</c>. This middleware short-circuits
/// the pipeline by streaming that file directly when markdown is preferred —
/// it does NOT rewrite <c>Request.Path</c> because rewriting after
/// <c>UseRouting()</c> would not re-trigger endpoint selection in
/// <c>MapStaticAssets()</c>.
/// it does NOT rewrite <c>Request.Path</c>, because the negotiated response needs
/// its own caching headers (below), which the site file middleware would replace.
/// </para>
/// <para>
/// Therefore the middleware MUST run before <c>UseDefaultFiles</c> and
Expand Down Expand Up @@ -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;
}

Expand Down
37 changes: 0 additions & 37 deletions src/statichost/StaticHost/Caching.targets

This file was deleted.

1 change: 1 addition & 0 deletions src/statichost/StaticHost/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/statichost/StaticHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
}

builder.AddLiveStatus();
builder.Services.AddSiteFiles();

builder.Services.AddHsts(options =>
{
Expand Down Expand Up @@ -43,8 +44,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();
Expand Down Expand Up @@ -92,6 +92,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.
Expand Down Expand Up @@ -139,6 +143,4 @@
return Results.Redirect("https://aka.ms/aspire/get/install.sh");
});

app.MapStaticAssets();

await app.RunAsync();
56 changes: 56 additions & 0 deletions src/statichost/StaticHost/Site.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<Project>
<!--
Publishes the site (src/frontend/dist) to wwwroot with a brotli copy of each text file.

StaticHost serves the web root with SiteFileMiddleware, not MapStaticAssets, so the project
turns static web assets off (StaticWebAssetsEnabled). That pipeline hashed every file, wrote a
manifest of every URL, and made each instance load it at startup, which costs a lot with
about 70,000 files. What it did that the site needs is here: run the frontend build, copy its
output, and compress it once at publish. Caching and ETags are decided per request.

Asking frontend.esproj for JavaScriptOutputGroup runs its BuildCommand and returns dist's
files, as the static web assets pipeline did. The brotli copies are written by a file-based
app (../compress-site.cs), which reuses the copies of files that did not change.
-->
<PropertyGroup>
<!-- The text types the SDK compressed (Microsoft.NET.Sdk.StaticWebAssets.Compression.targets). -->
<SiteCompressedExtensions>.html;.htm;.md;.markdown;.txt;.xml;.json;.webmanifest;.js;.mjs;.jsx;.css;.map;.svg;.ico;.bmp;.otf;.wasm;.csv;.tsv;.ics;.xsl;.xslt</SiteCompressedExtensions>
<SiteCompressApp>$(MSBuildThisFileDirectory)../compress-site.cs</SiteCompressApp>
<!-- The dotnet that runs this build, so the app uses the same SDK; plain dotnet under msbuild.exe. -->
<SiteDotnet>$(DOTNET_HOST_PATH)</SiteDotnet>
<SiteDotnet Condition="'$(SiteDotnet)' == ''">dotnet</SiteDotnet>
</PropertyGroup>

<Target Name="AddSiteToPublish" AfterTargets="ComputeResolvedFilesToPublishList">
<PropertyGroup>
<!-- Set here: IntermediateOutputPath is not defined yet when this file's properties are. -->
<_SiteIntermediateDirectory>$(MSBuildProjectDirectory)/$(IntermediateOutputPath)</_SiteIntermediateDirectory>
<_SiteCompressedDirectory>$(_SiteIntermediateDirectory)site-br/</_SiteCompressedDirectory>
<_SiteFileList>$(_SiteIntermediateDirectory)site-files.txt</_SiteFileList>
<_SiteBrotliList>$(_SiteIntermediateDirectory)site-br.txt</_SiteBrotliList>
</PropertyGroup>

<MSBuild Projects="@(ProjectReference->WithMetadataValue('Filename', 'frontend'))" Targets="JavaScriptOutputGroup">
<Output TaskParameter="TargetOutputs" ItemName="_SiteFile" />
</MSBuild>

<!-- The app reads each file's full path and its path in the site, separated by a tab. -->
<WriteLinesToFile File="$(_SiteFileList)" Lines="@(_SiteFile->'%(FullPath)%09%(TemplateSubPath)%(Filename)%(Extension)')" Overwrite="true" WriteOnlyWhenDifferent="true" />
<Exec Command="&quot;$(SiteDotnet)&quot; run --file &quot;$(SiteCompressApp)&quot; -- &quot;$(_SiteFileList)&quot; &quot;$(_SiteCompressedDirectory)&quot; &quot;$(SiteCompressedExtensions)&quot; &quot;$(_SiteBrotliList)&quot;"
StandardOutputImportance="high" />
<ReadLinesFromFile File="$(_SiteBrotliList)">
<Output TaskParameter="Lines" ItemName="_SiteBrotliFile" />
</ReadLinesFromFile>

<ItemGroup>
<ResolvedFileToPublish Include="@(_SiteFile)"
RelativePath="wwwroot/%(_SiteFile.TemplateSubPath)%(_SiteFile.Filename)%(_SiteFile.Extension)"
CopyToPublishDirectory="PreserveNewest"
ExcludeFromSingleFile="true" />
<ResolvedFileToPublish Include="@(_SiteBrotliFile->'$(_SiteCompressedDirectory)%(Identity)')"
RelativePath="wwwroot/%(_SiteBrotliFile.Identity)"
CopyToPublishDirectory="PreserveNewest"
ExcludeFromSingleFile="true" />
</ItemGroup>
</Target>
</Project>
65 changes: 65 additions & 0 deletions src/statichost/StaticHost/SiteFiles/SiteFileETags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.IO.Hashing;
using Microsoft.Extensions.FileProviders;

namespace StaticHost.SiteFiles;

/// <summary>
/// Computes a file's ETag from a hash of its content the first time it is served, then keeps it
/// in memory.
/// </summary>
/// <remarks>
/// 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
/// <c>UseStaticFiles</c> 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).
/// </remarks>
internal sealed class SiteFileETags
{
private const int BufferSize = 64 * 1024;

// 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<string, Entry> _entries = new(StringComparer.Ordinal);

/// <param name="path">The file's path in the web root, which identifies it whatever the provider.</param>
/// <param name="file">The file to hash.</param>
/// <param name="cancellationToken">Cancels the hashing.</param>
public async ValueTask<string> GetAsync(string path, IFileInfo file, CancellationToken cancellationToken)
{
var length = file.Length;
var lastModified = file.LastModified;

if (_entries.TryGetValue(path, 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.
var hash = new XxHash128();
var buffer = ArrayPool<byte>.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<byte>.Shared.Return(buffer);
}

var etag = $"\"{Convert.ToBase64String(hash.GetCurrentHash())}\"";
_entries[path] = new Entry(length, lastModified, etag);
return etag;
}

private sealed record Entry(long Length, DateTimeOffset LastModified, string ETag);
}
14 changes: 14 additions & 0 deletions src/statichost/StaticHost/SiteFiles/SiteFileExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace StaticHost.SiteFiles;

internal static class SiteFileExtensions
{
public static IServiceCollection AddSiteFiles(this IServiceCollection services) =>
services.AddSingleton<SiteFileETags>();

/// <summary>
/// Serves the web root's files, preferring the precompressed brotli copy. Call it after
/// <c>UseRouting</c>: it leaves requests that matched an endpoint to that endpoint.
/// </summary>
public static IApplicationBuilder UseSiteFiles(this IApplicationBuilder app) =>
app.UseMiddleware<SiteFileMiddleware>();
}
Loading
Loading