Voeg WebsiteMode.PrerenderedPages toe: nginx-gelijke serving van een voorgerenderde website
- Pure WebsitePathResolver (canonieke 301's, echte 404, dotfiles geweigerd, gereserveerde prefixes) - PrerenderedSiteMiddleware, 404-fallback na de API-routes, gzip, Cache-Control, NoIndex/robots.txt - Beleid StrictSite met eigen AllowedSite*Origins; volledige headerset op alle websiteresponses - Tests: voorbeelden, FsCheck-properties en integratietests op een in-memory server Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -40,7 +40,14 @@ public static class StaticContentExtensions
|
||||
/// Must be called BEFORE any middleware that needs to observe static responses, because
|
||||
/// static files short-circuit the pipeline.
|
||||
/// </summary>
|
||||
public static WebApplication UseCmsStaticContent(this WebApplication app)
|
||||
/// <param name="app">The application.</param>
|
||||
/// <param name="websiteMode">
|
||||
/// In <see cref="WebsiteMode.PrerenderedPages"/> only the admin mount is registered here; the
|
||||
/// website is served by <c>UseCmsPrerenderedSite</c> instead.
|
||||
/// </param>
|
||||
public static WebApplication UseCmsStaticContent(
|
||||
this WebApplication app,
|
||||
WebsiteMode websiteMode = WebsiteMode.SpaFallback)
|
||||
{
|
||||
var webRoot = app.Environment.WebRootPath
|
||||
?? Path.Combine(app.Environment.ContentRootPath, "wwwroot");
|
||||
@@ -52,7 +59,11 @@ public static class StaticContentExtensions
|
||||
.CreateLogger(typeof(StaticContentExtensions).FullName!);
|
||||
|
||||
WarnIfMissing(logger, adminRoot, "admin SPA");
|
||||
WarnIfMissing(logger, websiteRoot, "public website");
|
||||
|
||||
if (websiteMode == WebsiteMode.SpaFallback)
|
||||
{
|
||||
WarnIfMissing(logger, websiteRoot, "public website");
|
||||
}
|
||||
|
||||
// A request for exactly "/admin" must become "/admin/", or the SPA — built with
|
||||
// base '/admin/' — resolves its relative asset references one level too high and
|
||||
@@ -72,7 +83,11 @@ public static class StaticContentExtensions
|
||||
// The admin mount is registered FIRST. Registered the other way around, a request for
|
||||
// /admin/... would be resolved against the website root.
|
||||
RegisterMount(app, adminRoot, AdminRequestPath);
|
||||
RegisterMount(app, websiteRoot, requestPath: string.Empty);
|
||||
|
||||
if (websiteMode == WebsiteMode.SpaFallback)
|
||||
{
|
||||
RegisterMount(app, websiteRoot, requestPath: string.Empty);
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -85,7 +100,14 @@ public static class StaticContentExtensions
|
||||
/// looks like a file (has an extension) and does not exist must stay a 404. Serving HTML
|
||||
/// for a missing script would turn a clear "asset is missing" into a confusing parse error.
|
||||
/// </remarks>
|
||||
public static WebApplication MapCmsSpaFallbacks(this WebApplication app)
|
||||
/// <param name="app">The application.</param>
|
||||
/// <param name="websiteMode">
|
||||
/// In <see cref="WebsiteMode.PrerenderedPages"/> the catch-all website fallback is NOT mapped: an
|
||||
/// unknown path must be a real 404 (<c>MapCmsWebsiteNotFound</c>), never <c>index.html</c>.
|
||||
/// </param>
|
||||
public static WebApplication MapCmsSpaFallbacks(
|
||||
this WebApplication app,
|
||||
WebsiteMode websiteMode = WebsiteMode.SpaFallback)
|
||||
{
|
||||
var webRoot = app.Environment.WebRootPath
|
||||
?? Path.Combine(app.Environment.ContentRootPath, "wwwroot");
|
||||
@@ -105,6 +127,11 @@ public static class StaticContentExtensions
|
||||
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
});
|
||||
|
||||
if (websiteMode == WebsiteMode.PrerenderedPages)
|
||||
{
|
||||
return app;
|
||||
}
|
||||
|
||||
app.MapFallback("{*path:nonfile}", async context =>
|
||||
{
|
||||
if (File.Exists(websiteIndex))
|
||||
|
||||
Reference in New Issue
Block a user