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:
@@ -0,0 +1,73 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.ResponseCompression;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SlpModularCms.Core.Hosting.Website;
|
||||
|
||||
/// <summary>Registration of the prerendered-website serving (only used when <see cref="WebsiteMode.PrerenderedPages"/>).</summary>
|
||||
public static class PrerenderedSiteExtensions
|
||||
{
|
||||
/// <summary>Text types compressed on the website (same list as the site's nginx configuration).</summary>
|
||||
internal static readonly string[] CompressibleMimeTypes =
|
||||
[
|
||||
"text/html", "text/css", "text/plain", "text/xml", "text/javascript",
|
||||
"application/javascript", "application/json", "application/xml", "image/svg+xml"
|
||||
];
|
||||
|
||||
public static IServiceCollection AddCmsWebsite(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddOptions<WebsiteOptions>()
|
||||
.Bind(configuration.GetSection(WebsiteOptions.SectionName))
|
||||
.ValidateOnStart();
|
||||
|
||||
// Presence of the marker switches on the full security-header set for website responses.
|
||||
services.AddSingleton<WebsiteHostingMarker>();
|
||||
|
||||
services.AddSingleton(provider =>
|
||||
{
|
||||
var environment = provider.GetRequiredService<IWebHostEnvironment>();
|
||||
var webRoot = environment.WebRootPath ?? Path.Combine(environment.ContentRootPath, "wwwroot");
|
||||
|
||||
return new WebsiteFileAccess(
|
||||
Path.Combine(webRoot, StaticContentExtensions.WebsiteDirectoryName),
|
||||
provider.GetRequiredService<ILoggerFactory>().CreateLogger(PrerenderedSiteMiddleware.LoggerCategory));
|
||||
});
|
||||
|
||||
// gzip only, like nginx today; Brotli is deliberately not registered.
|
||||
services.AddResponseCompression(options =>
|
||||
{
|
||||
options.Providers.Add<GzipCompressionProvider>();
|
||||
options.MimeTypes = CompressibleMimeTypes;
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compression (non-reserved paths only) and the website middleware. Replaces the website static
|
||||
/// mount of <c>SpaFallback</c> mode; place it where <c>UseCmsStaticContent</c> sits.
|
||||
/// </summary>
|
||||
public static WebApplication UseCmsPrerenderedSite(this WebApplication app)
|
||||
{
|
||||
app.UseWhen(
|
||||
context => !WebsitePathResolver.IsReservedPath(context.Request.Path.Value),
|
||||
branch => branch.UseResponseCompression());
|
||||
|
||||
app.UseMiddleware<PrerenderedSiteMiddleware>();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
/// <summary>Maps the final fallback that serves <c>404.html</c> with status 404.</summary>
|
||||
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);
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user