Files
SluijsensandClaude Sonnet 5.5 ad0b114828 Hero from the CMS: markers HERO-HTML and HERO-PRELOAD, hero key in the data block, picture per ratio, baked fallback
The prerendered hero is plain HTML between the markers (first photo, h1, buttons), the slider takes over after hydration with the hero of the host block, the API or the build. The head preload is added to the finished HTML. Hero texts move into nl.json. The stand-in host and the e2e tests cover the hero.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 16:48:00 +01:00

126 lines
5.0 KiB
TypeScript

// A stand-in for what the CMS host does to a prerendered page: it fills the CMS
// markers from live data (the contract is in aidlc-docs/features/
// asf-cms-host-injection/plan.md and contract/host-injection). The real
// injector is C# in the CMS repo; this one lets the preview server and the
// browser tests show how the site behaves when a host rewrites the HTML, so
// hydration, fail-safe behaviour and layout can be checked without a host.
// It uses the site's own mapping and HTML code, so it proves the page copes
// with what the host writes; the golden files prove the host's template
// matches that code.
import { bannerHtml, offeringsHtml } from '../src/lib/cmsHtml.ts';
import { scriptJson } from '../src/lib/cmsData.ts';
import { mapHero } from '../src/lib/hero.ts';
import { heroHtml, heroPreloadHtml } from '../src/lib/heroHtml.ts';
import { formatPrice } from '../src/lib/formatPrice.ts';
import { mapBanner, mapOfferings, mapTopBanner } from '../src/lib/offerings.ts';
export interface LiveData {
// The answers as the public API gives them
offerings: unknown;
banners: { top: unknown; bottom: unknown };
// The answer of GET /api/v1/hero; absent or unusable leaves the baked hero
hero?: unknown;
}
export interface InjectOptions {
data: LiveData;
// The address the build was written with and the one to show instead
authoredBaseUrl?: string;
publicBaseUrl?: string;
}
// ld+json that cannot close its script element (same rule as the site's)
const jsonLd = (value: unknown) =>
JSON.stringify(value)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026');
const REGION =
/<!--CMS:(OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML|HERO-HTML|HERO-PRELOAD)-->[\s\S]*?<!--\/CMS:\1-->/g;
const DATA_BLOCK =
/<!--CMS:DATA-->(?:<script type="application\/json" id="cms-data">[\s\S]*?<\/script>)?/g;
const OFFER_JSON_LD =
/<!--CMS:OFFER-JSON-LD slug="([a-z0-9-]+)"--><script type="application\/ld\+json">([\s\S]*?)<\/script>/g;
const PRICE_SPAN =
/(<span data-cms-price="([a-z0-9-]+)"[^>]*>)[^<]*(<\/span>)/g;
type Price = Map<string, number>;
// Sets or removes offers on every node of a JSON-LD value that has them
function editOffers(value: unknown, price: number | undefined): void {
if (Array.isArray(value)) {
for (const item of value) editOffers(item, price);
return;
}
if (typeof value !== 'object' || value === null) return;
const node = value as Record<string, unknown>;
if ('offers' in node) {
if (price === undefined) delete node.offers;
else {
node.offers = { ...(node.offers as object), price };
}
}
for (const child of Object.values(node)) editOffers(child, price);
}
export function injectCms(html: string, options: InjectOptions): string {
const { data } = options;
const mapped = mapOfferings(data.offerings);
const packages = mapped ? mapped.packages : [];
const hero = mapHero(data.hero);
const prices: Price = new Map(packages.map((pkg) => [pkg.id, pkg.price]));
const regions: Record<string, string> = {
'OFFERINGS-HTML': offeringsHtml(packages),
'BANNER-TOP-HTML': bannerHtml(mapTopBanner(data.banners.top)),
'BANNER-BOTTOM-HTML': bannerHtml(mapBanner(data.banners.bottom)),
'HERO-HTML': heroHtml(hero),
'HERO-PRELOAD': heroPreloadHtml(hero),
};
const siteUrl = options.publicBaseUrl ?? options.authoredBaseUrl;
let out = html
.replace(REGION, (match, name: string) =>
// No usable hero: the baked one stays (fail open)
name.startsWith('HERO') && !hero
? match
: `<!--CMS:${name}-->${regions[name]}<!--/CMS:${name}-->`,
)
.replace(
DATA_BLOCK,
() =>
'<!--CMS:DATA-->' +
`<script type="application/json" id="cms-data">${scriptJson({
...(siteUrl ? { siteUrl } : {}),
offerings: data.offerings,
banners: data.banners,
...(data.hero === undefined ? {} : { hero: data.hero }),
})}</script>`,
)
.replace(OFFER_JSON_LD, (match, slug: string, json: string) => {
let parsed: unknown;
try {
parsed = JSON.parse(json);
} catch {
return match;
}
editOffers(parsed, prices.get(slug));
return `<script type="application/ld+json">${jsonLd(parsed)}</script>`;
})
.replace(
PRICE_SPAN,
(match, open: string, slug: string, close: string) => {
const price = prices.get(slug);
return price === undefined
? match
: `${open}${formatPrice(price)}${close}`;
},
);
const { authoredBaseUrl, publicBaseUrl } = options;
if (authoredBaseUrl && publicBaseUrl && authoredBaseUrl !== publicBaseUrl) {
out = out.split(authoredBaseUrl).join(publicBaseUrl);
}
return out;
}