Updates the packages and the banner live in the browser
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>
This commit is contained in:
@@ -8,7 +8,11 @@
|
||||
// https://test.andreaslop.nl (the API is same-origin, /api/v1/offerings).
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { mapOfferings, type SiteOfferings } from './offerings.ts';
|
||||
import {
|
||||
mapBanner,
|
||||
mapOfferings,
|
||||
type SiteOfferings,
|
||||
} from '../src/lib/offerings.ts';
|
||||
|
||||
const SNAPSHOT = fileURLToPath(
|
||||
new URL('../src/content/offerings.snapshot.json', import.meta.url),
|
||||
@@ -19,14 +23,17 @@ const GENERATED = fileURLToPath(
|
||||
const TIMEOUT_MS = 10_000;
|
||||
const ATTEMPTS = 2;
|
||||
|
||||
async function fetchAnswer(base: string): Promise<unknown> {
|
||||
const url = new URL('/api/v1/offerings', base);
|
||||
// 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) {
|
||||
@@ -45,13 +52,35 @@ async function main() {
|
||||
console.log('[offerings] no OFFERINGS_URL: using the snapshot');
|
||||
} else {
|
||||
try {
|
||||
const mapped = mapOfferings(await fetchAnswer(base));
|
||||
const mapped = mapOfferings(
|
||||
await fetchAnswer(base, '/api/v1/offerings'),
|
||||
);
|
||||
if (mapped) {
|
||||
result = { ...mapped, source: 'cms' };
|
||||
result = {
|
||||
...snapshot,
|
||||
packages: mapped.packages,
|
||||
source: 'cms',
|
||||
};
|
||||
console.log(
|
||||
`[offerings] ${mapped.packages.length} packages` +
|
||||
`${mapped.banner ? ' and a banner' : ''} from ${base}`,
|
||||
`[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`,
|
||||
|
||||
Reference in New Issue
Block a user