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:
2026-11-15 16:21:08 +01:00
co-authored by Claude Sonnet 5.5
parent 16027f5688
commit bf21b6f690
26 changed files with 1207 additions and 70 deletions
+64
View File
@@ -1,6 +1,9 @@
import { describe, expect, it } from 'vitest';
import {
bakedCmsData,
BUILD_SOURCE,
hostCmsData,
liveFromData,
parseCmsData,
rawBanner,
rawOfferings,
@@ -132,3 +135,64 @@ describe('the baked block', () => {
).toEqual(['newborn', 'gezin']);
});
});
const docWith = (json: string) =>
({
getElementById: () => ({ textContent: json }),
}) as unknown as Document;
describe('hostCmsData', () => {
it('is the block the host wrote', () => {
expect(hostCmsData(docWith('{"offerings":[]}'))?.offerings).toEqual([]);
});
it('is null for the block the build baked, and for no block', () => {
const baked = JSON.stringify(
bakedCmsData('https://andreaslop.nl', packages, null, null),
);
expect(parseCmsData(baked)?.source).toBe(BUILD_SOURCE);
expect(hostCmsData(docWith(baked))).toBeNull();
expect(
hostCmsData({ getElementById: () => null } as unknown as Document),
).toBeNull();
});
});
describe('liveFromData', () => {
it('gives nothing for no data', () => {
expect(liveFromData(null)).toBeNull();
});
it('takes only what the block has', () => {
expect(liveFromData({ siteUrl: 'https://andreaslop.nl' })).toEqual({});
});
it('trusts the host: an empty or unusable list is no packages', () => {
expect(liveFromData({ offerings: [] })).toEqual({ packages: [] });
expect(liveFromData({ offerings: [{ slug: 'Bad Slug' }] })).toEqual({
packages: [],
});
expect(
liveFromData({
offerings: [
{ slug: 'x', title: 'X', price: '€ 5', features: ['a'] },
],
})?.packages?.map((pkg) => pkg.id),
).toEqual(['x']);
});
it('maps both banners, null meaning none and an unusable answer too', () => {
expect(liveFromData({ banners: { top: null, bottom: null } })).toEqual({
topBanner: null,
banner: null,
});
expect(
liveFromData({
banners: { top: { text: 'Boven' }, bottom: { title: '' } },
}),
).toEqual({
topBanner: { title: undefined, text: 'Boven', link: undefined },
banner: null,
});
});
});
+45 -3
View File
@@ -1,7 +1,12 @@
import type { PricingBanner, PricingPackage } from '@/content/types';
import { formatPrice } from './formatPrice';
import type { OfferingTopBanner } from './offerings';
import { resolveSiteUrl } from './siteUrl';
import { formatPrice } from './formatPrice.ts';
import {
mapBanner,
mapOfferings,
mapTopBanner,
type OfferingTopBanner,
} from './offerings.ts';
import { resolveSiteUrl } from './siteUrl.ts';
// The data island of a page: what the CMS host writes into
// <script type="application/json" id="cms-data"> after the <!--CMS:DATA-->
@@ -12,6 +17,9 @@ import { resolveSiteUrl } from './siteUrl';
// and the page keeps what it was built with.
export interface CmsData {
// "build" in the block the build baked; absent in what the host writes. A
// baked block is the data the page was built with, not live data.
source?: string;
// Already checked by resolveSiteUrl
siteUrl?: string;
offerings?: unknown;
@@ -35,6 +43,7 @@ export function parseCmsData(text: string | null | undefined): CmsData | null {
? { top: raw.banners.top, bottom: raw.banners.bottom }
: undefined;
return {
source: typeof raw.source === 'string' ? raw.source : undefined,
siteUrl:
typeof raw.siteUrl === 'string' && raw.siteUrl !== ''
? resolveSiteUrl(raw.siteUrl)
@@ -45,6 +54,7 @@ export function parseCmsData(text: string | null | undefined): CmsData | null {
}
export const CMS_DATA_ID = 'cms-data';
export const BUILD_SOURCE = 'build';
// The block of the page, or null without a usable one (a preview, a test, a
// host that does not inject)
@@ -54,6 +64,15 @@ export function readCmsData(
return parseCmsData(doc.getElementById(CMS_DATA_ID)?.textContent);
}
// The block the CMS host wrote, or null when the page has none or only the one
// the build baked (a host that does not inject): then the page asks the API
export function hostCmsData(
doc: Pick<Document, 'getElementById'>,
): CmsData | null {
const data = readCmsData(doc);
return data?.source === BUILD_SOURCE ? null : data;
}
// JSON that cannot end the script element or break a line terminator rule:
// < > & and U+2028/2029 as \u escapes (the host does the same)
export function scriptJson(value: unknown): string {
@@ -103,6 +122,7 @@ export function bakedCmsData(
topBanner: OfferingTopBanner | null,
): CmsData {
return {
source: BUILD_SOURCE,
siteUrl,
offerings: rawOfferings(packages),
banners: {
@@ -111,3 +131,25 @@ export function bakedCmsData(
},
};
}
// What the page shows from a data block: only the parts the block has. The
// host's list is authoritative, so an empty list means no packages (the host
// switched them all off); null for a banner means none; a part that is not in
// the block, or a banner answer that cannot be used, leaves the baked one.
export interface LiveFromData {
packages?: PricingPackage[];
banner?: PricingBanner | null;
topBanner?: OfferingTopBanner | null;
}
export function liveFromData(data: CmsData | null): LiveFromData | null {
if (!data) return null;
const live: LiveFromData = {};
if (data.offerings !== undefined) {
live.packages = mapOfferings(data.offerings)?.packages ?? [];
}
const { top, bottom } = data.banners ?? {};
if (bottom !== undefined) live.banner = mapBanner(bottom);
if (top !== undefined) live.topBanner = mapTopBanner(top);
return live;
}
+2 -2
View File
@@ -1,6 +1,6 @@
import type { PricingBanner, PricingPackage } from '@/content/types';
import type { OfferingTopBanner } from './offerings';
import { formatPrice } from './formatPrice';
import type { OfferingTopBanner } from './offerings.ts';
import { formatPrice } from './formatPrice.ts';
// The plain, crawlable HTML that sits between the CMS markers in the
// prerendered pages. The host (SLP Modular CMS) writes the same markup from