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

78 lines
3.0 KiB
TypeScript

// Makes a prerendered page and the same page after the CMS host filled its
// markers comparable. The deployment check (deploy/verify-deployment.sh)
// compares what the server sends with the file in dist; since the host
// writes the current packages, banners, prices and address into the page,
// those parts are taken out of both sides first. Everything else must still be
// byte for byte equal, so an old build or a wrong server config is still
// caught. The rules mirror the host contract in
// aidlc-docs/features/asf-cms-host-injection/plan.md.
//
// Usage: node config/normalizeInjected.ts <input.html> <output.html> [site-url ...]
import { readFileSync, writeFileSync } from 'node:fs';
const REGION =
/(<!--CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML|HERO-HTML|HERO-PRELOAD)-->)[\s\S]*?(<!--\/CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML|HERO-HTML|HERO-PRELOAD)-->)/g;
const DATA_BLOCK =
/(?:<!--CMS:DATA-->)?<script type="application\/json" id="cms-data">[\s\S]*?<\/script>|<!--CMS:DATA-->/g;
const OFFER_MARKER = /<!--CMS:OFFER-JSON-LD slug="[a-z0-9-]+"-->/g;
const LD_JSON =
/(<script type="application\/ld\+json">)([\s\S]*?)(<\/script>)/g;
const PRICE_SPAN = /(<span data-cms-price="[a-z0-9-]+"[^>]*>)[^<]*(<\/span>)/g;
// JSON-LD: parse, drop every "offers" (the host rewrites or removes them),
// write it back the same way for both sides
function dropOffers(value: unknown): unknown {
if (Array.isArray(value)) return value.map(dropOffers);
if (typeof value !== 'object' || value === null) return value;
return Object.fromEntries(
Object.entries(value)
.filter(([key]) => key !== 'offers')
.map(([key, child]) => [key, dropOffers(child)]),
);
}
export function normalizeInjected(
html: string,
siteUrls: readonly string[] = [],
): string {
let out = html
.replace(REGION, '$1$2')
.replace(DATA_BLOCK, '')
.replace(OFFER_MARKER, '')
.replace(PRICE_SPAN, '$1$2')
.replace(
LD_JSON,
(match, open: string, json: string, close: string) => {
try {
return `${open}${JSON.stringify(dropOffers(JSON.parse(json)))}${close}`;
} catch {
// Not JSON: leave it, so a broken block still shows up as a
// difference
return match;
}
},
);
for (const url of siteUrls) out = out.split(url).join('@SITE@');
return out;
}
function main(args: string[]) {
const [input, output, ...siteUrls] = args;
if (!input || !output) {
console.error(
'Gebruik: node config/normalizeInjected.ts <in.html> <uit.html> [site-url ...]',
);
process.exitCode = 2;
return;
}
writeFileSync(
output,
normalizeInjected(readFileSync(input, 'utf8'), siteUrls),
);
}
// Only when run as a script, not when a test imports it
if (process.argv[1]?.endsWith('normalizeInjected.ts')) {
main(process.argv.slice(2));
}