Files
AndreaSlopFotografie/web/config/normalizeInjected.ts
T
SluijsensandClaude Sonnet 5.5 d9ff875ade Lets the deployment check cope with pages the CMS host has filled
The check after a deploy compared the server's answer with dist byte for byte,
which fails once the host writes current data into the markers. Both sides are
now normalised first (regions, data block, offers, inline prices, site
address); everything else still has to match. The stand-in host no longer
injects the 404 page, like the real one.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 17:02:17 +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)-->)[\s\S]*?(<!--\/CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML)-->)/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));
}