()
.CreateLogger(typeof(StaticContentExtensions).FullName!);
WarnIfMissing(logger, adminRoot, "admin SPA");
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
// fails in a way that looks like a broken deployment.
app.Use(async (context, next) =>
{
if (context.Request.Path.Equals(AdminRequestPath, StringComparison.OrdinalIgnoreCase))
{
var target = $"{AdminRequestPath}/{context.Request.QueryString}";
context.Response.Redirect(target, permanent: true);
return;
}
await next();
});
// 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);
if (websiteMode == WebsiteMode.SpaFallback)
{
RegisterMount(app, websiteRoot, requestPath: string.Empty);
}
return app;
}
///
/// Maps the SPA fallbacks for both mounts.
///
///
/// The nonfile constraint on both routes is deliberate: a request for a path that
/// 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.
///
/// The application.
///
/// In the catch-all website fallback is NOT mapped: an
/// unknown path must be a real 404 (MapCmsWebsiteNotFound), never index.html.
///
public static WebApplication MapCmsSpaFallbacks(
this WebApplication app,
WebsiteMode websiteMode = WebsiteMode.SpaFallback)
{
var webRoot = app.Environment.WebRootPath
?? Path.Combine(app.Environment.ContentRootPath, "wwwroot");
var adminIndex = Path.Combine(webRoot, AdminDirectoryName, "index.html");
var websiteIndex = Path.Combine(webRoot, WebsiteDirectoryName, "index.html");
app.MapFallback($"{AdminRequestPath}/{{*path:nonfile}}", async context =>
{
if (File.Exists(adminIndex))
{
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync(adminIndex);
return;
}
context.Response.StatusCode = StatusCodes.Status404NotFound;
});
if (websiteMode == WebsiteMode.PrerenderedPages)
{
return app;
}
app.MapFallback("{*path:nonfile}", async context =>
{
if (File.Exists(websiteIndex))
{
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync(websiteIndex);
return;
}
// No website deployed yet. Serving the placeholder rather than a 404 makes a fresh
// installation self-explanatory and doubles as proof the CMS itself is running.
await WritePlaceholderAsync(context);
});
return app;
}
private static void RegisterMount(WebApplication app, string physicalRoot, string requestPath)
{
// Tolerating a missing directory is required, not defensive: a fresh deployment has no
// wwwroot/web/ until a website workspace deploys into it, and the CMS must still start
// and serve /admin and /api/v1.
if (!Directory.Exists(physicalRoot))
{
return;
}
var provider = new PhysicalFileProvider(physicalRoot);
app.UseDefaultFiles(new DefaultFilesOptions
{
FileProvider = provider,
RequestPath = requestPath
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = provider,
RequestPath = requestPath
// Directory browsing is not enabled — a request for a directory resolves to its
// default file or falls through to the fallback rules, never to a file listing.
});
}
private static async Task WritePlaceholderAsync(HttpContext context)
{
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = "text/html; charset=utf-8";
// Embedded in the assembly rather than placed in wwwroot/web/, because that directory is
// owned and overwritten by a website workspace: a file there would be deleted by the first
// real website deployment, or mistaken for part of the customer's site.
await using var stream = typeof(StaticContentExtensions).Assembly
.GetManifestResourceStream(PlaceholderResourceName);
if (stream is null)
{
await context.Response.WriteAsync("Nog geen website geplaatst" +
"Er staat hier nog geen website. Beheer via /admin/.
");
return;
}
await stream.CopyToAsync(context.Response.Body);
}
private static void WarnIfMissing(ILogger logger, string path, string description)
{
if (Directory.Exists(path))
{
return;
}
// Normal on a fresh installation, but on a running production instance it means the
// content has disappeared — worth being visible in Sentry without blocking startup.
logger.LogWarning(
"Static content directory for the {Description} was not found at {Path}. " +
"The application will start, but this path will not serve any files until content is deployed there.",
description,
path);
}
}