From 284ff1f91adc14057e2ee69254fe5b233b52db9a Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Wed, 11 Nov 2026 23:48:48 +0100 Subject: [PATCH] U6: IndexHtmlInjector (markers OFFERINGS-JSON-LD / OFFERINGS-HTML), 60s cache, invalidatie bij wijziging, ETag Co-Authored-By: Claude Sonnet 5.5 --- .../Hosting/Website/SpaSeoTests.cs | 72 ++++++++++- .../Hosting/StaticContentExtensions.cs | 26 ++++ .../Hosting/Website/IndexHtmlInjector.cs | 106 ++++++++++++++++ .../Hosting/Website/SpaSeoExtensions.cs | 2 + .../Services/OfferingsIndexMarkersTests.cs | 85 +++++++++++++ .../Services/OfferingsServiceTests.cs | 17 +++ .../OfferingsModule.cs | 2 + .../Services/OfferingsIndexMarkers.cs | 114 ++++++++++++++++++ .../Services/OfferingsService.cs | 8 +- 9 files changed, 430 insertions(+), 2 deletions(-) create mode 100644 src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs create mode 100644 src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsIndexMarkersTests.cs create mode 100644 src/SlpModularCms.Modules.Offerings/Services/OfferingsIndexMarkers.cs diff --git a/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs b/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs index d9832f2..46068a1 100644 --- a/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs +++ b/src/SlpModularCms.Core.Tests/Hosting/Website/SpaSeoTests.cs @@ -34,7 +34,76 @@ public sealed class SpaSeoTests : IDisposable if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); } - private async Task StartAsync(Dictionary settings) + private sealed class CountingProvider : IIndexHtmlMarkerProvider + { + public int Calls; + public string Value = "one"; + public bool Fail; + + public Task> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken) + { + Interlocked.Increment(ref Calls); + if (Fail) throw new InvalidOperationException("db down"); + return Task.FromResult>( + new Dictionary { ["SLP:TEST"] = $"[{Value}@{publicBaseUrl}]" }); + } + } + + private async Task<(HttpClient Client, CountingProvider Provider, IIndexHtmlCache Cache)> StartWithMarkersAsync() + { + File.WriteAllText(Path.Combine(_root, "wwwroot", "web", "index.html"), "
"); + var provider = new CountingProvider(); + var client = await StartAsync(Config(noIndex: false, baseUrl: "https://slpsoftware.nl"), services => services.AddSingleton(provider)); + return (client, provider, _apps[^1].Services.GetRequiredService()); + } + + [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(""); + 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("").And.Contain("id=\"root\""); + } + + private async Task StartAsync(Dictionary settings, Action? 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(); diff --git a/src/SlpModularCms.Core/Hosting/StaticContentExtensions.cs b/src/SlpModularCms.Core/Hosting/StaticContentExtensions.cs index 8152745..7e52f89 100644 --- a/src/SlpModularCms.Core/Hosting/StaticContentExtensions.cs +++ b/src/SlpModularCms.Core/Hosting/StaticContentExtensions.cs @@ -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(); + 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; } diff --git a/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs b/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs new file mode 100644 index 0000000..82e967d --- /dev/null +++ b/src/SlpModularCms.Core/Hosting/Website/IndexHtmlInjector.cs @@ -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; + +/// +/// A module's contribution to the website's index.html: replacement text for marker comments such as +/// <!--SLP:OFFERINGS-HTML-->. The key is the marker name without the comment delimiters +/// (SLP:OFFERINGS-HTML); the value is the HTML that replaces the whole comment (empty = remove it). +/// +public interface IIndexHtmlMarkerProvider +{ + Task> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken); +} + +/// Lets a module say that the markers' content changed (the page is rebuilt on the next request). +public interface IIndexHtmlCache +{ + void Invalidate(); +} + +public sealed record InjectedPage(byte[] Body, string ETag); + +/// +/// Fills the marker comments of the SpaFallback website's index.html from the registered +/// s. The rendered page is cached for per public base URL +/// and dropped at once on (a module calls it after a change) or when the file changes. +/// +public sealed class IndexHtmlInjector( + IServiceScopeFactory scopes, + IOptions options, + ILogger 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 _cache = new(); + private long _version; + + private sealed record Entry(long Version, DateTime FileTime, DateTimeOffset Expires, InjectedPage? Page); + + public void Invalidate() => Interlocked.Increment(ref _version); + + /// The page with its markers filled, or null when no provider is registered (serve the file as it is). + public async Task 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().ToList(); + if (providers.Count == 0) + { + _cache[baseUrl] = new Entry(version, fileTime, now + CacheTtl, null); + return null; + } + + var markers = new Dictionary(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($"", 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; + } + } +} diff --git a/src/SlpModularCms.Core/Hosting/Website/SpaSeoExtensions.cs b/src/SlpModularCms.Core/Hosting/Website/SpaSeoExtensions.cs index fa35233..507881f 100644 --- a/src/SlpModularCms.Core/Hosting/Website/SpaSeoExtensions.cs +++ b/src/SlpModularCms.Core/Hosting/Website/SpaSeoExtensions.cs @@ -22,6 +22,8 @@ public static class SpaSeoExtensions public static IServiceCollection AddCmsSpaSeo(this IServiceCollection services, IConfiguration configuration) { services.AddOptions().Bind(configuration.GetSection(WebsiteOptions.SectionName)); + services.AddSingleton(); + services.AddSingleton(provider => provider.GetRequiredService()); return services; } diff --git a/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsIndexMarkersTests.cs b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsIndexMarkersTests.cs new file mode 100644 index 0000000..49595b3 --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsIndexMarkersTests.cs @@ -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(""); + var doc = JsonDocument.Parse(json["".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("x & y")], "https://x.nl"); + + json.Substring("", string.Empty, StringComparison.Ordinal) + .Should().NotContain("<"); + } + + [Fact] + public void Html_IsASemanticList_WithEverythingEncoded() + { + var html = OfferingsIndexMarkers.Html([Dto("A & B", price: "€ 300", note: "eenmalig")]); + + html.Should().Be( + "
  • A <b>& B</b>

    " + + "

    Beschrijving

    € 300 eenmalig

"); + } + + [Fact] + public async Task WithoutOfferings_BothMarkersAreEmpty() + { + var service = Substitute.For(); + 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(); + } +} diff --git a/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsServiceTests.cs b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsServiceTests.cs index c142f5f..53b9101 100644 --- a/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsServiceTests.cs +++ b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsServiceTests.cs @@ -267,6 +267,23 @@ public class OfferingsServiceTests offering.DeletedAt.Should().NotBeNull(); } + [Fact] + public async Task EveryChange_InvalidatesTheWebsitePageCache() + { + var cache = Substitute.For(); + var sut = new OfferingsService(_repo, Substitute.For(), _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( diff --git a/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs b/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs index 649b6db..e5188c6 100644 --- a/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs +++ b/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs @@ -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(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddHttpContextAccessor(); diff --git a/src/SlpModularCms.Modules.Offerings/Services/OfferingsIndexMarkers.cs b/src/SlpModularCms.Modules.Offerings/Services/OfferingsIndexMarkers.cs new file mode 100644 index 0000000..0c60aab --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings/Services/OfferingsIndexMarkers.cs @@ -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; + +/// +/// Fills the website's <!--SLP:OFFERINGS-JSON-LD--> and <!--SLP:OFFERINGS-HTML--> 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 GET /api/v1/offerings) is host-independent: a build-time variant fills +/// the same markers from the same JSON. +/// +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> RenderAsync(string publicBaseUrl, CancellationToken cancellationToken) + { + var list = await offerings.GetOfferingsAsync(); + return new Dictionary + { + [JsonLdMarker] = JsonLd(list, publicBaseUrl), + [HtmlMarker] = Html(list), + }; + } + + [GeneratedRegex(@"€ ?([0-9.,]+)")] + private static partial Regex EuroPrice(); + + /// + /// 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. + /// + 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 list, string baseUrl) + { + if (list.Count == 0) + { + return string.Empty; + } + + var items = list.Select((o, index) => + { + var offer = new Dictionary + { + ["@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 + { + ["@type"] = "ListItem", + ["position"] = index + 1, + ["item"] = offer, + }; + }).ToList(); + + var document = new Dictionary + { + ["@context"] = "https://schema.org", + ["@type"] = "ItemList", + ["itemListElement"] = items, + }; + + // The default encoder escapes <, > and &, so the JSON can never close the script element. + return ""; + } + + internal static string Html(IReadOnlyList list) + { + if (list.Count == 0) + { + return string.Empty; + } + + var html = new StringBuilder("
    "); + foreach (var o in list) + { + var priceLine = string.Join(' ', new[] { o.Price, o.PriceNote }.Where(part => !string.IsNullOrWhiteSpace(part))); + html.Append("
  • ").Append(WebUtility.HtmlEncode(o.Title)).Append("

    ") + .Append(WebUtility.HtmlEncode(o.Description)).Append("

    ") + .Append(WebUtility.HtmlEncode(priceLine)).Append("

  • "); + } + + return html.Append("
").ToString(); + } +} diff --git a/src/SlpModularCms.Modules.Offerings/Services/OfferingsService.cs b/src/SlpModularCms.Modules.Offerings/Services/OfferingsService.cs index d415f78..426f35b 100644 --- a/src/SlpModularCms.Modules.Offerings/Services/OfferingsService.cs +++ b/src/SlpModularCms.Modules.Offerings/Services/OfferingsService.cs @@ -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 logger) : IOfferingsService + ILogger logger, + IIndexHtmlCache? indexCache = null) : IOfferingsService { public async Task> 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);