Builds the markers, data block and stand-in host for the CMS host injection
The pages carry the CMS markers (data block, list regions, JSON-LD price, inline price); PackagesProvider reads the host's data block after hydration and asks the API only when there is none; the site address comes from the block. A stand-in for the host (config/hostInjector.ts, --cms-data in the preview server) and browser tests check the behaviour with different data. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
// 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 { 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 };
|
||||
}
|
||||
|
||||
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)-->[\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 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)),
|
||||
};
|
||||
const siteUrl = options.publicBaseUrl ?? options.authoredBaseUrl;
|
||||
|
||||
let out = html
|
||||
.replace(
|
||||
REGION,
|
||||
(_match, name: string) =>
|
||||
`<!--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,
|
||||
})}</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;
|
||||
}
|
||||
Reference in New Issue
Block a user