U6: IndexHtmlInjector (markers OFFERINGS-JSON-LD / OFFERINGS-HTML), 60s cache, invalidatie bij wijziging, ETag

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 23:48:48 +01:00
co-authored by Claude Sonnet 5.5
parent a7460a0f9b
commit 284ff1f91a
9 changed files with 430 additions and 2 deletions
@@ -34,7 +34,76 @@ public sealed class SpaSeoTests : IDisposable
if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true);
}
private async Task<HttpClient> StartAsync(Dictionary<string, string?> settings)
private sealed class CountingProvider : IIndexHtmlMarkerProvider
{
public int Calls;
public string Value = "one";
public bool Fail;
public Task<IReadOnlyDictionary<string, string>> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken)
{
Interlocked.Increment(ref Calls);
if (Fail) throw new InvalidOperationException("db down");
return Task.FromResult<IReadOnlyDictionary<string, string>>(
new Dictionary<string, string> { ["SLP:TEST"] = $"[{Value}@{publicBaseUrl}]" });
}
}
private async Task<(HttpClient Client, CountingProvider Provider, IIndexHtmlCache Cache)> StartWithMarkersAsync()
{
File.WriteAllText(Path.Combine(_root, "wwwroot", "web", "index.html"), "<body><!--SLP:TEST--><div id=\"root\"></div></body>");
var provider = new CountingProvider();
var client = await StartAsync(Config(noIndex: false, baseUrl: "https://slpsoftware.nl"), services => services.AddSingleton<IIndexHtmlMarkerProvider>(provider));
return (client, provider, _apps[^1].Services.GetRequiredService<IIndexHtmlCache>());
}
[Fact]
public async Task Markers_AreFilled_CachedAndRefreshedOnInvalidate_WithAnETag()
{
var (client, provider, cache) = await StartWithMarkersAsync();
var first = await client.GetAsync("/");
(await first.Content.ReadAsStringAsync()).Should().Contain("[one@https://slpsoftware.nl]").And.NotContain("<!--SLP:TEST-->");
first.Headers.CacheControl!.NoCache.Should().BeTrue();
var etag = first.Headers.ETag!;
provider.Value = "two";
(await client.GetStringAsync("/privacy")).Should().Contain("[one@"); // served from the cache
provider.Calls.Should().Be(1);
cache.Invalidate();
var second = await client.GetAsync("/");
(await second.Content.ReadAsStringAsync()).Should().Contain("[two@");
second.Headers.ETag.Should().NotBe(etag);
provider.Calls.Should().Be(2);
}
[Fact]
public async Task AMatchingETag_Gets304()
{
var (client, _, _) = await StartWithMarkersAsync();
var etag = (await client.GetAsync("/")).Headers.ETag!;
var request = new HttpRequestMessage(HttpMethod.Get, "/");
request.Headers.IfNoneMatch.Add(etag);
var response = await client.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.NotModified);
}
[Fact]
public async Task WhenAProviderFails_ThePlainPageIsStillServed()
{
var (client, provider, _) = await StartWithMarkersAsync();
provider.Fail = true;
var response = await client.GetAsync("/");
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("<!--SLP:TEST-->").And.Contain("id=\"root\"");
}
private async Task<HttpClient> StartAsync(Dictionary<string, string?> settings, Action<IServiceCollection>? configure = null)
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
@@ -48,6 +117,7 @@ public sealed class SpaSeoTests : IDisposable
builder.Configuration.AddInMemoryCollection(settings);
builder.Services.AddCmsSecurityHeaders(builder.Configuration);
builder.Services.AddCmsSpaSeo(builder.Configuration);
configure?.Invoke(builder.Services);
var app = builder.Build();
app.UseCmsSecurityHeaders();
@@ -148,6 +148,32 @@ public static class StaticContentExtensions
}
context.Response.ContentType = "text/html; charset=utf-8";
// Modules fill marker comments in index.html (e.g. the current offerings); cached, with an ETag.
var injector = context.RequestServices.GetService<IndexHtmlInjector>();
var page = injector is null
? null
: await injector.GetAsync(websiteIndex, context.Request, context.RequestAborted);
if (page is not null)
{
context.Response.Headers["Cache-Control"] = "no-cache";
context.Response.Headers["ETag"] = page.ETag;
if (context.Response.StatusCode == StatusCodes.Status200OK
&& context.Request.Headers.IfNoneMatch.Contains(page.ETag))
{
context.Response.StatusCode = StatusCodes.Status304NotModified;
return;
}
context.Response.ContentLength = page.Body.Length;
if (!HttpMethods.IsHead(context.Request.Method))
{
await context.Response.Body.WriteAsync(page.Body, context.RequestAborted);
}
return;
}
await context.Response.SendFileAsync(websiteIndex);
return;
}
@@ -0,0 +1,106 @@
using System.Collections.Concurrent;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace SlpModularCms.Core.Hosting.Website;
/// <summary>
/// A module's contribution to the website's <c>index.html</c>: replacement text for marker comments such as
/// <c>&lt;!--SLP:OFFERINGS-HTML--&gt;</c>. The key is the marker name without the comment delimiters
/// (<c>SLP:OFFERINGS-HTML</c>); the value is the HTML that replaces the whole comment (empty = remove it).
/// </summary>
public interface IIndexHtmlMarkerProvider
{
Task<IReadOnlyDictionary<string, string>> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken);
}
/// <summary>Lets a module say that the markers' content changed (the page is rebuilt on the next request).</summary>
public interface IIndexHtmlCache
{
void Invalidate();
}
public sealed record InjectedPage(byte[] Body, string ETag);
/// <summary>
/// Fills the marker comments of the SpaFallback website's <c>index.html</c> from the registered
/// <see cref="IIndexHtmlMarkerProvider"/>s. The rendered page is cached for <see cref="CacheTtl"/> per public base URL
/// and dropped at once on <see cref="Invalidate"/> (a module calls it after a change) or when the file changes.
/// </summary>
public sealed class IndexHtmlInjector(
IServiceScopeFactory scopes,
IOptions<WebsiteOptions> options,
ILogger<IndexHtmlInjector> logger,
TimeProvider? time = null) : IIndexHtmlCache
{
public static readonly TimeSpan CacheTtl = TimeSpan.FromSeconds(60);
private static readonly TimeSpan FailureTtl = TimeSpan.FromSeconds(5);
private readonly TimeProvider _time = time ?? TimeProvider.System;
private readonly ConcurrentDictionary<string, Entry> _cache = new();
private long _version;
private sealed record Entry(long Version, DateTime FileTime, DateTimeOffset Expires, InjectedPage? Page);
public void Invalidate() => Interlocked.Increment(ref _version);
/// <summary>The page with its markers filled, or null when no provider is registered (serve the file as it is).</summary>
public async Task<InjectedPage?> GetAsync(string indexPath, HttpRequest request, CancellationToken cancellationToken)
{
var baseUrl = SpaSeoExtensions.BaseUrl(options.Value) ?? $"{request.Scheme}://{request.Host}";
var fileTime = File.GetLastWriteTimeUtc(indexPath);
var version = Interlocked.Read(ref _version);
var now = _time.GetUtcNow();
if (_cache.TryGetValue(baseUrl, out var cached)
&& cached.Version == version && cached.FileTime == fileTime && cached.Expires > now)
{
return cached.Page;
}
try
{
using var scope = scopes.CreateScope();
var providers = scope.ServiceProvider.GetServices<IIndexHtmlMarkerProvider>().ToList();
if (providers.Count == 0)
{
_cache[baseUrl] = new Entry(version, fileTime, now + CacheTtl, null);
return null;
}
var markers = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (var provider in providers)
{
foreach (var (name, replacement) in await provider.RenderAsync(baseUrl, cancellationToken))
{
markers[name] = replacement;
}
}
var html = await File.ReadAllTextAsync(indexPath, cancellationToken);
foreach (var (name, replacement) in markers)
{
html = html.Replace($"<!--{name}-->", replacement, StringComparison.Ordinal);
}
var body = Encoding.UTF8.GetBytes(html);
var etag = "\"" + Convert.ToHexString(SHA256.HashData(body))[..32].ToLowerInvariant() + "\"";
var page = new InjectedPage(body, etag);
_cache[baseUrl] = new Entry(version, fileTime, now + CacheTtl, page);
return page;
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Never take the website down for its dynamic parts: keep the last good page if there is one, else
// serve the file with its markers untouched (they are HTML comments, so they render as nothing).
logger.LogError(ex, "Filling the website markers failed; serving the previous or the plain page");
var page = cached?.Page;
_cache[baseUrl] = new Entry(version, fileTime, now + FailureTtl, page);
return page;
}
}
}
@@ -22,6 +22,8 @@ public static class SpaSeoExtensions
public static IServiceCollection AddCmsSpaSeo(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<WebsiteOptions>().Bind(configuration.GetSection(WebsiteOptions.SectionName));
services.AddSingleton<IndexHtmlInjector>();
services.AddSingleton<IIndexHtmlCache>(provider => provider.GetRequiredService<IndexHtmlInjector>());
return services;
}
@@ -0,0 +1,85 @@
using System.Text.Json;
using FluentAssertions;
using NSubstitute;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Services;
namespace SlpModularCms.Modules.Offerings.Tests.Services;
public class OfferingsIndexMarkersTests
{
private static OfferingDto Dto(string title, string price = "€ 300", string note = "eenmalig", string layout = "card") => new(
Guid.NewGuid().ToString(), "slug", title, "Beschrijving", price, note, ["a"], "Start", false, 0, null, null, layout);
[Theory]
[InlineData("€ 300", "300")]
[InlineData("€300", "300")]
[InlineData("€ 1.500,50", "1500.50")]
[InlineData("€ 1.500", "1500")]
[InlineData("€ 49,-", "49")]
[InlineData("vanaf € 79,95 p/m", "79.95")]
[InlineData("Op aanvraag", null)]
[InlineData("", null)]
[InlineData("$ 300", null)]
public void ParsePrice_HandlesDutchNotation_AndNeverGuesses(string label, string? expected)
=> OfferingsIndexMarkers.ParsePrice(label).Should().Be(expected);
[Fact]
public void JsonLd_IsAnItemListOfOffers_WithPositions_AndTheBannerIncluded()
{
var json = OfferingsIndexMarkers.JsonLd([Dto("Start"), Dto("Banner", layout: "banner")], "https://slpsoftware.nl");
json.Should().StartWith("<script type=\"application/ld+json\">").And.EndWith("</script>");
var doc = JsonDocument.Parse(json["<script type=\"application/ld+json\">".Length..^"</script>".Length]).RootElement;
doc.GetProperty("@context").GetString().Should().Be("https://schema.org");
doc.GetProperty("@type").GetString().Should().Be("ItemList");
var items = doc.GetProperty("itemListElement").EnumerateArray().ToList();
items.Should().HaveCount(2);
items[1].GetProperty("position").GetInt32().Should().Be(2);
var offer = items[0].GetProperty("item");
offer.GetProperty("@type").GetString().Should().Be("Offer");
offer.GetProperty("name").GetString().Should().Be("Start");
offer.GetProperty("url").GetString().Should().Be("https://slpsoftware.nl/#pakketten");
offer.GetProperty("price").GetString().Should().Be("300");
offer.GetProperty("priceCurrency").GetString().Should().Be("EUR");
}
[Fact]
public void JsonLd_OmitsPriceAndCurrency_WhenTheLabelIsNotParseable()
{
var json = OfferingsIndexMarkers.JsonLd([Dto("Maatwerk", price: "Op aanvraag")], "https://x.nl");
json.Should().NotContain("\"price\"").And.NotContain("priceCurrency");
}
[Fact]
public void JsonLd_CannotCloseTheScriptElement()
{
var json = OfferingsIndexMarkers.JsonLd([Dto("</script><b>x</b> & y")], "https://x.nl");
json.Substring("<script type=\"application/ld+json\">".Length).Replace("</script>", string.Empty, StringComparison.Ordinal)
.Should().NotContain("<");
}
[Fact]
public void Html_IsASemanticList_WithEverythingEncoded()
{
var html = OfferingsIndexMarkers.Html([Dto("A <b>& B</b>", price: "€ 300", note: "eenmalig")]);
html.Should().Be(
"<section id=\"packages-static\" aria-label=\"Pakketten\"><ul><li><h3>A &lt;b&gt;&amp; B&lt;/b&gt;</h3>" +
"<p>Beschrijving</p><p>€ 300 eenmalig</p></li></ul></section>");
}
[Fact]
public async Task WithoutOfferings_BothMarkersAreEmpty()
{
var service = Substitute.For<IOfferingsService>();
service.GetOfferingsAsync().Returns([]);
var markers = await new OfferingsIndexMarkers(service).RenderAsync("https://x.nl", CancellationToken.None);
markers[OfferingsIndexMarkers.JsonLdMarker].Should().BeEmpty();
markers[OfferingsIndexMarkers.HtmlMarker].Should().BeEmpty();
}
}
@@ -267,6 +267,23 @@ public class OfferingsServiceTests
offering.DeletedAt.Should().NotBeNull();
}
[Fact]
public async Task EveryChange_InvalidatesTheWebsitePageCache()
{
var cache = Substitute.For<SlpModularCms.Core.Hosting.Website.IIndexHtmlCache>();
var sut = new OfferingsService(_repo, Substitute.For<IOfferingCategoryRepository>(), _logger, cache);
var offering = ExistingOffering();
_repo.GetByIdAsync(offering.Id).Returns(offering);
_repo.GetAllAsync().Returns([offering]);
await sut.CreateAsync(CreateRequest(), Guid.NewGuid());
await sut.UpdateAsync(offering.Id, UpdateRequestWithLayout(null), Guid.NewGuid());
await sut.ReorderAsync([offering.Id], Guid.NewGuid());
await sut.DeleteAsync(offering.Id, Guid.NewGuid());
cache.Received(4).Invalidate();
}
// --- Layout (card/banner), at most one banner ---
private static CreateOfferingRequest CreateRequestWithLayout(string? layout) => new(
@@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Core.Hosting;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Modules;
using SlpModularCms.Modules.Offerings.Data;
using SlpModularCms.Modules.Offerings.Repositories;
@@ -29,6 +30,7 @@ public class OfferingsModule : IModule
services.AddScoped<IOfferingRepository, OfferingRepository>();
services.AddScoped<IOfferingsService, OfferingsService>();
services.AddScoped<IIndexHtmlMarkerProvider, OfferingsIndexMarkers>();
services.AddScoped<IOfferingCategoryRepository, OfferingCategoryRepository>();
services.AddScoped<IOfferingCategoriesService, OfferingCategoriesService>();
services.AddHttpContextAccessor();
@@ -0,0 +1,114 @@
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Modules.Offerings.Models;
namespace SlpModularCms.Modules.Offerings.Services;
/// <summary>
/// Fills the website's <c>&lt;!--SLP:OFFERINGS-JSON-LD--&gt;</c> and <c>&lt;!--SLP:OFFERINGS-HTML--&gt;</c> markers with the
/// current offerings (banner included): a schema.org ItemList of Offers for search engines and a plain, semantic,
/// non-hydrated list that the React app removes at start-up. Both are empty when there are no offerings.
/// The contract (markers + the JSON of <c>GET /api/v1/offerings</c>) is host-independent: a build-time variant fills
/// the same markers from the same JSON.
/// </summary>
public sealed partial class OfferingsIndexMarkers(IOfferingsService offerings) : IIndexHtmlMarkerProvider
{
public const string JsonLdMarker = "SLP:OFFERINGS-JSON-LD";
public const string HtmlMarker = "SLP:OFFERINGS-HTML";
public async Task<IReadOnlyDictionary<string, string>> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken)
{
var list = await offerings.GetOfferingsAsync();
return new Dictionary<string, string>
{
[JsonLdMarker] = JsonLd(list, publicBaseUrl),
[HtmlMarker] = Html(list),
};
}
[GeneratedRegex(@"€ ?([0-9.,]+)")]
private static partial Regex EuroPrice();
/// <summary>
/// The numeric price of a label like "€ 1.500,50" (Dutch notation: dot = thousands, comma = decimals), as
/// "1500.50"; null when the label is not parseable. Never guessed.
/// </summary>
internal static string? ParsePrice(string label)
{
var match = EuroPrice().Match(label ?? string.Empty);
if (!match.Success)
{
return null;
}
var digits = match.Groups[1].Value.Trim('.', ',').Replace(".", string.Empty).Replace(',', '.');
return decimal.TryParse(digits, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var value)
? value.ToString(CultureInfo.InvariantCulture)
: null;
}
internal static string JsonLd(IReadOnlyList<OfferingDto> list, string baseUrl)
{
if (list.Count == 0)
{
return string.Empty;
}
var items = list.Select((o, index) =>
{
var offer = new Dictionary<string, object>
{
["@type"] = "Offer",
["name"] = o.Title,
["description"] = o.Description,
["url"] = baseUrl + "/#pakketten",
};
if (ParsePrice(o.Price) is { } price)
{
offer["price"] = price;
offer["priceCurrency"] = "EUR";
}
return new Dictionary<string, object>
{
["@type"] = "ListItem",
["position"] = index + 1,
["item"] = offer,
};
}).ToList();
var document = new Dictionary<string, object>
{
["@context"] = "https://schema.org",
["@type"] = "ItemList",
["itemListElement"] = items,
};
// The default encoder escapes <, > and &, so the JSON can never close the script element.
return "<script type=\"application/ld+json\">" + JsonSerializer.Serialize(document) + "</script>";
}
internal static string Html(IReadOnlyList<OfferingDto> list)
{
if (list.Count == 0)
{
return string.Empty;
}
var html = new StringBuilder("<section id=\"packages-static\" aria-label=\"Pakketten\"><ul>");
foreach (var o in list)
{
var priceLine = string.Join(' ', new[] { o.Price, o.PriceNote }.Where(part => !string.IsNullOrWhiteSpace(part)));
html.Append("<li><h3>").Append(WebUtility.HtmlEncode(o.Title)).Append("</h3><p>")
.Append(WebUtility.HtmlEncode(o.Description)).Append("</p><p>")
.Append(WebUtility.HtmlEncode(priceLine)).Append("</p></li>");
}
return html.Append("</ul></section>").ToString();
}
}
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Modules.Offerings.Data.Entities;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Repositories;
@@ -8,7 +9,8 @@ namespace SlpModularCms.Modules.Offerings.Services;
public class OfferingsService(
IOfferingRepository repository,
IOfferingCategoryRepository categories,
ILogger<OfferingsService> logger) : IOfferingsService
ILogger<OfferingsService> logger,
IIndexHtmlCache? indexCache = null) : IOfferingsService
{
public async Task<IReadOnlyList<OfferingDto>> GetOfferingsAsync()
{
@@ -59,6 +61,7 @@ public class OfferingsService(
await repository.SaveChangesAsync();
}
indexCache?.Invalidate();
logger.LogInformation(
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
offering.Id, "Created", callerId);
@@ -112,6 +115,7 @@ public class OfferingsService(
await repository.SaveChangesAsync();
}
indexCache?.Invalidate();
logger.LogInformation(
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
offering.Id, "Updated", callerId);
@@ -134,6 +138,7 @@ public class OfferingsService(
repository.Update(offering);
await repository.SaveChangesAsync();
indexCache?.Invalidate();
logger.LogInformation(
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
offering.Id, "Deleted", callerId);
@@ -171,6 +176,7 @@ public class OfferingsService(
await repository.SaveChangesAsync();
await transaction.CommitAsync();
indexCache?.Invalidate();
logger.LogInformation(
"Offerings reordered ({Count} items) by user {LastModifiedByUserId}",
orderedIds.Count, callerId);