The build still bakes the current packages and the active banner into the HTML. After hydration the page asks /api/v1/offerings and /api/v1/offerings/banner once and replaces what differs; errors and time-outs keep what was baked, and a 204 from the banner route hides the banner. The banner is now the OfferingBanner contract (title, text, optional link) instead of layout=banner. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lines
3.6 KiB
TypeScript
99 lines
3.6 KiB
TypeScript
// Build step, runs before the build: asks the CMS for the shoot packages and
|
|
// writes src/content/offerings.generated.json, which the site imports. With
|
|
// no address, a failing CMS or an unusable answer the committed snapshot
|
|
// (src/content/offerings.snapshot.json) is used, so the build never fails
|
|
// and never shows empty prices. The log says which one was used.
|
|
//
|
|
// OFFERINGS_URL: the site's own address that serves the API, for example
|
|
// https://test.andreaslop.nl (the API is same-origin, /api/v1/offerings).
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import {
|
|
mapBanner,
|
|
mapOfferings,
|
|
type SiteOfferings,
|
|
} from '../src/lib/offerings.ts';
|
|
|
|
const SNAPSHOT = fileURLToPath(
|
|
new URL('../src/content/offerings.snapshot.json', import.meta.url),
|
|
);
|
|
const GENERATED = fileURLToPath(
|
|
new URL('../src/content/offerings.generated.json', import.meta.url),
|
|
);
|
|
const TIMEOUT_MS = 10_000;
|
|
const ATTEMPTS = 2;
|
|
|
|
// The JSON answer, or undefined for 204 (no content). Other statuses and
|
|
// network errors throw.
|
|
async function fetchAnswer(base: string, path: string): Promise<unknown> {
|
|
const url = new URL(path, base);
|
|
for (let attempt = 1; ; attempt++) {
|
|
try {
|
|
const response = await fetch(url, {
|
|
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
headers: { Accept: 'application/json' },
|
|
});
|
|
if (response.status === 204) return undefined;
|
|
if (!response.ok) throw new Error(`status ${response.status}`);
|
|
return await response.json();
|
|
} catch (error) {
|
|
if (attempt >= ATTEMPTS) throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const snapshot = JSON.parse(
|
|
readFileSync(SNAPSHOT, 'utf8'),
|
|
) as SiteOfferings;
|
|
const base = process.env.OFFERINGS_URL?.trim();
|
|
let result = { ...snapshot, source: 'snapshot' };
|
|
if (!base) {
|
|
console.log('[offerings] no OFFERINGS_URL: using the snapshot');
|
|
} else {
|
|
try {
|
|
const mapped = mapOfferings(
|
|
await fetchAnswer(base, '/api/v1/offerings'),
|
|
);
|
|
if (mapped) {
|
|
result = {
|
|
...snapshot,
|
|
packages: mapped.packages,
|
|
source: 'cms',
|
|
};
|
|
console.log(
|
|
`[offerings] ${mapped.packages.length} packages from ${base}`,
|
|
);
|
|
// The banner: 200 is the active one, 204 means none. A
|
|
// failing banner call keeps the snapshot's banner (none).
|
|
try {
|
|
const banner = mapBanner(
|
|
await fetchAnswer(base, '/api/v1/offerings/banner'),
|
|
);
|
|
result.banner = banner;
|
|
console.log(
|
|
banner
|
|
? `[offerings] banner "${banner.title}"`
|
|
: '[offerings] no active banner',
|
|
);
|
|
} catch (error) {
|
|
console.warn(
|
|
`[offerings] banner not available (${String(error)}): none`,
|
|
);
|
|
}
|
|
} else {
|
|
console.warn(
|
|
`[offerings] answer of ${base} has no valid package: using the snapshot`,
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.warn(
|
|
`[offerings] ${base} not reachable (${String(error)}): using the snapshot`,
|
|
);
|
|
}
|
|
}
|
|
writeFileSync(GENERATED, JSON.stringify(result, null, 4) + '\n');
|
|
}
|
|
|
|
await main();
|