H4: hero in de eerste HTML (regio, preload), gedeeld datablok met contributors (Offerings byte-identiek), ASF golden files, volledige pijplijntest
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,8 @@ public class OfferingsModule : IModule
|
||||
services.AddScoped<IOfferingsService, OfferingsService>();
|
||||
services.AddScoped<IIndexHtmlMarkerProvider, OfferingsIndexMarkers>();
|
||||
services.AddScoped<IHtmlPageTransform, OfferingsHtmlTransform>();
|
||||
services.AddScoped<ICmsDataBlockContributor, OfferingsDataContributor>();
|
||||
services.AddScoped<ICmsDataBlockContributor, BannersDataContributor>();
|
||||
services.AddScoped<IOfferingBannerRepository, OfferingBannerRepository>();
|
||||
services.AddScoped<IOfferingBannersService, OfferingBannersService>();
|
||||
services.AddScoped<IOfferingCategoryRepository, OfferingCategoryRepository>();
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
/// <summary>The <c>offerings</c> key of the page data block: the same list the public endpoint answers.</summary>
|
||||
public sealed class OfferingsDataContributor(IOfferingsService offerings) : ICmsDataBlockContributor
|
||||
{
|
||||
public string Key => "offerings";
|
||||
|
||||
public int Order => 100;
|
||||
|
||||
public async Task<object?> GetAsync(HtmlTransformContext context, CancellationToken cancellationToken)
|
||||
=> await offerings.GetOfferingsAsync();
|
||||
}
|
||||
|
||||
/// <summary>The <c>banners</c> key of the page data block: the active top and bottom banner (null when there is none).</summary>
|
||||
public sealed class BannersDataContributor(IOfferingBannersService banners) : ICmsDataBlockContributor
|
||||
{
|
||||
public string Key => "banners";
|
||||
|
||||
public int Order => 110;
|
||||
|
||||
public async Task<object?> GetAsync(HtmlTransformContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
var top = await banners.GetActiveAsync(OfferingBannerPositions.Top);
|
||||
var bottom = await banners.GetActiveAsync(OfferingBannerPositions.Bottom);
|
||||
return new { top, bottom };
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,9 @@ public sealed class OfferingsHtmlTransform(IOfferingsService offerings, IOfferin
|
||||
public const string OfferingsRegion = "CMS:OFFERINGS-HTML";
|
||||
public const string BannerTopRegion = "CMS:BANNER-TOP-HTML";
|
||||
public const string BannerBottomRegion = "CMS:BANNER-BOTTOM-HTML";
|
||||
public const string DataMarker = "CMS:DATA";
|
||||
public const string DataBlockId = "cms-data";
|
||||
public const string JsonLdMarker = "CMS:OFFER-JSON-LD";
|
||||
public const string PriceAttribute = "data-cms-price";
|
||||
|
||||
// The shape of GET /api/v1/offerings and /offerings/banner: camelCase, nulls written.
|
||||
private static readonly JsonSerializerOptions ApiJson = new(JsonSerializerDefaults.Web);
|
||||
|
||||
private static readonly JsonSerializerOptions LdJson = new()
|
||||
{
|
||||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
@@ -35,13 +30,11 @@ public sealed class OfferingsHtmlTransform(IOfferingsService offerings, IOfferin
|
||||
|
||||
public async Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
var wantsData = html.Contains("<!--" + DataMarker + "-->", StringComparison.Ordinal);
|
||||
var wantsOfferings = wantsData
|
||||
|| html.Contains("<!--" + OfferingsRegion + "-->", StringComparison.Ordinal)
|
||||
var wantsOfferings = html.Contains("<!--" + OfferingsRegion + "-->", StringComparison.Ordinal)
|
||||
|| html.Contains("<!--" + JsonLdMarker, StringComparison.Ordinal)
|
||||
|| html.Contains(PriceAttribute + "=", StringComparison.Ordinal);
|
||||
var wantsTop = wantsData || html.Contains("<!--" + BannerTopRegion + "-->", StringComparison.Ordinal);
|
||||
var wantsBottom = wantsData || html.Contains("<!--" + BannerBottomRegion + "-->", StringComparison.Ordinal);
|
||||
var wantsTop = html.Contains("<!--" + BannerTopRegion + "-->", StringComparison.Ordinal);
|
||||
var wantsBottom = html.Contains("<!--" + BannerBottomRegion + "-->", StringComparison.Ordinal);
|
||||
|
||||
var live = wantsOfferings ? await offerings.GetOfferingsAsync() : [];
|
||||
var top = wantsTop ? await banners.GetActiveAsync(OfferingBannerPositions.Top) : null;
|
||||
@@ -65,22 +58,9 @@ public sealed class OfferingsHtmlTransform(IOfferingsService offerings, IOfferin
|
||||
html = HtmlMarkerEditor.ReplaceRegion(html, BannerBottomRegion, CmsPageHtml.Banner(bottom, OfferingBannerPositions.Bottom));
|
||||
}
|
||||
|
||||
if (wantsData)
|
||||
{
|
||||
html = HtmlMarkerEditor.ReplaceDataBlock(html, DataMarker, DataBlockId, DataBlock(context.SiteUrl, live, top, bottom));
|
||||
}
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/// <summary>The data block: the answers of the public API plus the address of the site, safe inside a script element.</summary>
|
||||
public static string DataBlock(string siteUrl, IReadOnlyList<OfferingDto> live, PublicOfferingBannerDto? top, PublicOfferingBannerDto? bottom)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(
|
||||
new { siteUrl, offerings = live, banners = new { top, bottom } }, ApiJson);
|
||||
return "<script type=\"application/json\" id=\"" + DataBlockId + "\">" + HtmlMarkerEditor.EscapeForScript(json) + "</script>";
|
||||
}
|
||||
|
||||
private static int? FindPrice(IReadOnlyList<OfferingDto> live, string slug)
|
||||
=> live.FirstOrDefault(o => string.Equals(o.Slug, slug, StringComparison.Ordinal)) is { } offering
|
||||
? CmsPageHtml.WholeEuros(offering.Price)
|
||||
|
||||
Reference in New Issue
Block a user