Website: host vervangt het productie-basisadres in index.html door Website__PublicBaseUrl op testinstanties (Website:AuthoredBaseUrl)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,9 @@
|
|||||||
// robots.txt then allows crawling and points to /sitemap.xml.
|
// robots.txt then allows crawling and points to /sitemap.xml.
|
||||||
"NoIndex": true,
|
"NoIndex": true,
|
||||||
"PublicBaseUrl": "",
|
"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" ] },
|
"Sitemap": { "Paths": [ "/", "/privacy" ] },
|
||||||
// Only these website paths are pages (200); any other path is a real 404. /api, /admin and assets keep working.
|
// Only these website paths are pages (200); any other path is a real 404. /api, /admin and assets keep working.
|
||||||
"KnownPaths": [ "/", "/privacy" ]
|
"KnownPaths": [ "/", "/privacy" ]
|
||||||
|
|||||||
@@ -78,6 +78,43 @@ public sealed class SpaSeoTests : IDisposable
|
|||||||
provider.Calls.Should().Be(2);
|
provider.Calls.Should().Be(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<string> ServeAuthoredPageAsync(string baseUrl, string? authored)
|
||||||
|
{
|
||||||
|
File.WriteAllText(
|
||||||
|
Path.Combine(_root, "wwwroot", "web", "index.html"),
|
||||||
|
"<head><link rel=\"canonical\" href=\"https://slpsoftware.nl/\"><meta property=\"og:image\" content=\"https://slpsoftware.nl/og.png\"></head><body><!--SLP:TEST--></body>");
|
||||||
|
var settings = Config(noIndex: false, baseUrl: baseUrl);
|
||||||
|
if (authored is not null) settings["Website:AuthoredBaseUrl"] = authored;
|
||||||
|
var client = await StartAsync(settings, services => services.AddSingleton<IIndexHtmlMarkerProvider>(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]
|
[Fact]
|
||||||
public async Task AMatchingETag_Gets304()
|
public async Task AMatchingETag_Gets304()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -82,6 +82,18 @@ public sealed class IndexHtmlInjector(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var html = await File.ReadAllTextAsync(indexPath, cancellationToken);
|
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)
|
foreach (var (name, replacement) in markers)
|
||||||
{
|
{
|
||||||
html = html.Replace($"<!--{name}-->", replacement, StringComparison.Ordinal);
|
html = html.Replace($"<!--{name}-->", replacement, StringComparison.Ordinal);
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ public sealed class WebsiteOptions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string? PublicBaseUrl { get; set; }
|
public string? PublicBaseUrl { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The production address the website's static <c>index.html</c> is authored with (canonical, og:url, og:image,
|
||||||
|
/// twitter:image, JSON-LD url), e.g. <c>https://slpsoftware.nl</c>. When it differs from
|
||||||
|
/// <see cref="PublicBaseUrl"/> (a test instance), the host replaces it by <see cref="PublicBaseUrl"/> while
|
||||||
|
/// serving <c>index.html</c>, so a test site points at itself. Empty = no replacement; on production it is a no-op.
|
||||||
|
/// </summary>
|
||||||
|
public string? AuthoredBaseUrl { get; set; }
|
||||||
|
|
||||||
/// <summary>Paths listed in <c>/sitemap.xml</c> (SpaFallback sites), e.g. <c>/</c> and <c>/privacy</c>.</summary>
|
/// <summary>Paths listed in <c>/sitemap.xml</c> (SpaFallback sites), e.g. <c>/</c> and <c>/privacy</c>.</summary>
|
||||||
public SitemapOptions Sitemap { get; set; } = new();
|
public SitemapOptions Sitemap { get; set; } = new();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user