From 32b98fea982debaa077fc6b0dc1c8bd66a0204b0 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sun, 15 Nov 2026 11:44:02 +0100 Subject: [PATCH] Website: host vervangt het productie-basisadres in index.html door Website__PublicBaseUrl op testinstanties (Website:AuthoredBaseUrl) Co-Authored-By: Claude Sonnet 5.5 --- .../appsettings.json | 3 ++ .../Hosting/Website/SpaSeoTests.cs | 37 +++++++++++++++++++ .../Hosting/Website/IndexHtmlInjector.cs | 12 ++++++ .../Hosting/Website/WebsiteOptions.cs | 8 ++++ 4 files changed, 60 insertions(+) diff --git a/src/SlpModularCms.Api.SlpSoftware/appsettings.json b/src/SlpModularCms.Api.SlpSoftware/appsettings.json index ee11036..9945066 100644 --- a/src/SlpModularCms.Api.SlpSoftware/appsettings.json +++ b/src/SlpModularCms.Api.SlpSoftware/appsettings.json @@ -15,6 +15,9 @@ // robots.txt then allows crawling and points to /sitemap.xml. "NoIndex": true, "PublicBaseUrl": "", + // The address the static index.html is written with. On a test instance (PublicBaseUrl set to its own address) + // the host swaps it for PublicBaseUrl; on production both are equal, so nothing changes. + "AuthoredBaseUrl": "https://slpsoftware.nl", "Sitemap": { "Paths": [ "/", "/privacy" ] }, // Only these website paths are pages (200); any other path is a real 404. /api, /admin and assets keep working. "KnownPaths": [ "/", "/privacy" ] diff --git a/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs b/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs index 46068a1..ec3b043 100644 --- a/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs +++ b/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs @@ -78,6 +78,43 @@ public sealed class SpaSeoTests : IDisposable provider.Calls.Should().Be(2); } + private async Task ServeAuthoredPageAsync(string baseUrl, string? authored) + { + File.WriteAllText( + Path.Combine(_root, "wwwroot", "web", "index.html"), + ""); + var settings = Config(noIndex: false, baseUrl: baseUrl); + if (authored is not null) settings["Website:AuthoredBaseUrl"] = authored; + var client = await StartAsync(settings, services => services.AddSingleton(new CountingProvider())); + return await client.GetStringAsync("/"); + } + + [Fact] + public async Task OnATestInstance_TheAuthoredProductionAddressBecomesTheInstancesOwn() + { + var html = await ServeAuthoredPageAsync("https://test.slpsoftware.nl", "https://slpsoftware.nl"); + + html.Should().Contain("href=\"https://test.slpsoftware.nl/\"") + .And.Contain("content=\"https://test.slpsoftware.nl/og.png\"") + .And.NotContain("https://slpsoftware.nl"); + } + + [Fact] + public async Task OnProduction_TheReplacementIsANoOp() + { + var html = await ServeAuthoredPageAsync("https://slpsoftware.nl", "https://slpsoftware.nl"); + + html.Should().Contain("href=\"https://slpsoftware.nl/\"").And.Contain("https://slpsoftware.nl/og.png"); + } + + [Fact] + public async Task WithoutAnAuthoredAddress_NothingIsReplaced() + { + var html = await ServeAuthoredPageAsync("https://test.slpsoftware.nl", null); + + html.Should().Contain("https://slpsoftware.nl/og.png"); + } + [Fact] public async Task AMatchingETag_Gets304() { diff --git a/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs b/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs index 82e967d..6dc8f14 100644 --- a/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs +++ b/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs @@ -82,6 +82,18 @@ public sealed class IndexHtmlInjector( } var html = await File.ReadAllTextAsync(indexPath, cancellationToken); + + // A test instance points at itself: the authored production address becomes this instance's own. + // Only a configured PublicBaseUrl counts (never one derived from the request's Host header). + var configured = SpaSeoExtensions.BaseUrl(options.Value); + var authored = Uri.TryCreate(options.Value.AuthoredBaseUrl?.Trim(), UriKind.Absolute, out var authoredUri) + ? authoredUri.GetLeftPart(UriPartial.Authority) + : null; + if (configured is not null && authored is not null + && !string.Equals(configured, authored, StringComparison.OrdinalIgnoreCase)) + { + html = html.Replace(authored, configured, StringComparison.Ordinal); + } foreach (var (name, replacement) in markers) { html = html.Replace($"", replacement, StringComparison.Ordinal); diff --git a/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs b/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs index cd421f0..9c85df3 100644 --- a/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs +++ b/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs @@ -25,6 +25,14 @@ public sealed class WebsiteOptions /// public string? PublicBaseUrl { get; set; } + /// + /// The production address the website's static index.html is authored with (canonical, og:url, og:image, + /// twitter:image, JSON-LD url), e.g. https://slpsoftware.nl. When it differs from + /// (a test instance), the host replaces it by while + /// serving index.html, so a test site points at itself. Empty = no replacement; on production it is a no-op. + /// + public string? AuthoredBaseUrl { get; set; } + /// Paths listed in /sitemap.xml (SpaFallback sites), e.g. / and /privacy. public SitemapOptions Sitemap { get; set; } = new();