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:
+45
-3
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user