Herstel: admin-bestanden geven 404 in PrerenderedPages-modus

De catch-all 404-route van de website werd ook voor /admin/... gekozen, waardoor de
static-file-middleware (die routes met endpoint overslaat) de adminbestanden niet
serveerde. Een routeconstraint houdt de gereserveerde prefixes buiten die route.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 11:12:13 +01:00
co-authored by Claude Sonnet 5.5
parent 05fe0f1f0e
commit d0c1d26c5c
4 changed files with 101 additions and 2 deletions
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace SlpModularCms.Core.Hosting.Website;
/// <summary>
/// Route constraint for the website's catch-all 404 route: it refuses the reserved prefixes
/// (<c>/admin</c>, <c>/api/v1</c>, <c>/health</c>).
/// </summary>
/// <remarks>
/// Without it the catch-all (which deliberately has no 'nonfile' constraint, so a missing
/// <c>/assets/x.js</c> gets the 404 page) is selected for every request, including
/// <c>/admin/assets/x.css</c>. The static-file middleware skips requests that already have an
/// endpoint, so the admin's files would 404. With the constraint the reserved prefixes behave exactly
/// as in <see cref="WebsiteMode.SpaFallback"/>: the admin keeps its own 'nonfile' fallback, an unknown
/// file under <c>/admin</c> is a plain 404 from the static layer, an unknown <c>/api/v1/x</c> stays
/// an empty 404.
/// </remarks>
public sealed class NotReservedPathConstraint : IRouteConstraint
{
public const string Name = "notreserved";
public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values,
RouteDirection routeDirection)
{
if (!values.TryGetValue(routeKey, out var value) || value is null)
{
return true;
}
return !WebsitePathResolver.IsReservedPath("/" + value);
}
}
@@ -37,6 +37,10 @@ public static class PrerenderedSiteExtensions
provider.GetRequiredService<ILoggerFactory>().CreateLogger(PrerenderedSiteMiddleware.LoggerCategory));
});
// Lets the catch-all 404 route refuse the reserved prefixes (see NotReservedPathConstraint).
services.Configure<RouteOptions>(options =>
options.ConstraintMap[NotReservedPathConstraint.Name] = typeof(NotReservedPathConstraint));
// gzip only, like nginx today; Brotli is deliberately not registered.
services.AddResponseCompression(options =>
{
@@ -66,8 +70,9 @@ public static class PrerenderedSiteExtensions
public static WebApplication MapCmsWebsiteNotFound(this WebApplication app)
{
// An explicit pattern: the default fallback pattern carries the "nonfile" constraint, and a
// missing asset (/assets/x.js) must also get the 404 page, not a bare framework 404.
app.MapFallback("{*path}", WebsiteNotFoundHandler.HandleAsync);
// missing asset (/assets/x.js) must also get the 404 page, not a bare framework 404. The
// reserved prefixes are excluded so they keep their own behaviour (see NotReservedPathConstraint).
app.MapFallback($"{{*path:{NotReservedPathConstraint.Name}}}", WebsiteNotFoundHandler.HandleAsync);
return app;
}
}