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
@@ -0,0 +1,24 @@
import packages, { banner, topBanner } from '@/content/packages';
import { bakedCmsData, CMS_DATA_ID, scriptJson } from '@/lib/cmsData';
import { siteUrl } from '@/seo';
// The data block of the page, in the root document: <!--CMS:DATA--> followed
// by the JSON of the packages and banners the build baked. The CMS host
// replaces the pair with the current data; the browser reads it after
// hydration (PackagesProvider) and the address from it before (client.tsx).
// Hidden and never hydrated: the host's block differs from the baked one.
export default function CmsDataIsland() {
const json = scriptJson(bakedCmsData(siteUrl, packages, banner, topBanner));
return (
<div
hidden
data-cms-island
suppressHydrationWarning
dangerouslySetInnerHTML={{
__html:
'<!--CMS:DATA-->' +
`<script type="application/json" id="${CMS_DATA_ID}">${json}</script>`,
}}
/>
);
}
@@ -0,0 +1,51 @@
import { renderToString } from 'react-dom/server';
import { screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { renderWithProviders } from '@/test/renderWithProviders';
import CmsDataIsland from './CmsDataIsland';
import CmsRegion from './CmsRegion';
describe('CmsRegion', () => {
it('is plain HTML between two markers on the server', () => {
const html = renderToString(
<CmsRegion name="OFFERINGS-HTML" html="<ul><li>x</li></ul>">
<p>gestijld</p>
</CmsRegion>,
);
expect(html).toContain(
'<!--CMS:OFFERINGS-HTML--><ul><li>x</li></ul><!--/CMS:OFFERINGS-HTML-->',
);
expect(html).not.toContain('gestijld');
});
it('shows the styled content once it runs in the browser', async () => {
await renderWithProviders(
<CmsRegion name="OFFERINGS-HTML" html="<ul></ul>">
<p>gestijld</p>
</CmsRegion>,
);
expect(screen.getByText('gestijld')).toBeInTheDocument();
});
});
describe('CmsDataIsland', () => {
it('writes the marker and the baked block, hidden, with the build as source', () => {
const html = renderToString(<CmsDataIsland />);
expect(html).toMatch(
/<div hidden="" data-cms-island="true"><!--CMS:DATA--><script type="application\/json" id="cms-data">\{/,
);
const json = /id="cms-data">(.*?)<\/script>/.exec(html)?.[1] ?? '';
const data = JSON.parse(json) as {
source: string;
siteUrl: string;
offerings: unknown[];
banners: object;
};
expect(data.source).toBe('build');
expect(data.siteUrl).toBe('https://andreaslop.nl');
expect(data.offerings.length).toBeGreaterThan(0);
expect(Object.keys(data.banners)).toEqual(['top', 'bottom']);
// Nothing in it can end the script element
expect(json).not.toMatch(/[<>&]/);
});
});
@@ -0,0 +1,32 @@
import type { ReactNode } from 'react';
import useHydrated from '@/hooks/useHydrated';
import { regionMarkers } from '@/lib/cmsHtml';
type CmsRegionProps = {
// Marker name, for example "OFFERINGS-HTML": <!--CMS:OFFERINGS-HTML-->
name: string;
// The plain HTML of the data the page was built with (lib/cmsHtml); the
// CMS host replaces it with the same markup from the current data
html: string;
// What the visitor gets once the page has hydrated: the styled
// components, built from the current data
children: ReactNode;
};
// A part of the page that the CMS host may rewrite before it sends the HTML
// (packages, banners). Until hydration it is plain HTML between two marker
// comments, which React leaves alone (a host that wrote different data would
// otherwise cause a hydration mismatch). After hydration the styled
// components replace it. React cannot write comments, so the markers go in
// through dangerouslySetInnerHTML; the html is escaped by lib/cmsHtml and
// never contains anything but what that escapes.
export default function CmsRegion({ name, html, children }: CmsRegionProps) {
if (useHydrated()) return <>{children}</>;
return (
<div
data-cms-region={name}
suppressHydrationWarning
dangerouslySetInnerHTML={{ __html: regionMarkers(name, html) }}
/>
);
}