Heritage, Bold, Community and Clean drawing all 26 section variants with their headers, homepage order and style settings; colours made safe (WCAG AA) by the backend; fonts and corners from the shared brand style. Publishing into numbered versions, the content API with live messages and events and the church profile from Accounts, uploads, every page kind, simple event pages, Coming soon, 404, sitemap, SEO basics and the church's JSON-LD. The demo church (npm run demo) and Playwright e2e: every template's pages pass axe and the SEO checks. website.sh builds packages/seo. Decision 17 (first Owner) recorded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.1 KiB
TypeScript
72 lines
3.1 KiB
TypeScript
/**
|
|
* The church profile (`crosscode.church/1`) from the church's Accounts (WEBSITE-1.0-PLAN §2): name,
|
|
* logo, colours, brand style, service times, contact, social, the church's other sites. Accounts
|
|
* owns it; the Website shows it and never stores it (§2: shared facts aren't drafts). As Events
|
|
* 3.7's accounts/church.ts: kept in memory for 10 minutes, the last copy kept while Accounts can't
|
|
* be reached (tried again after a minute).
|
|
*
|
|
* Before the Website is linked to Accounts (and in local development) the profile saved in
|
|
* AppSetting `church_profile_local` is used, if any (`npm run demo` saves the demo church's).
|
|
*/
|
|
import { CHURCH_PATHS, churchSchema, type Church } from '@crosscode/contracts';
|
|
import type { Ctx } from '../context.js';
|
|
import { accountsLink } from '../auth/accounts.js';
|
|
|
|
const FRESH_MS = 10 * 60e3;
|
|
const RETRY_MS = 60e3;
|
|
const TIMEOUT_MS = 5000;
|
|
|
|
type Cached = { url: string; church: Church | null; at: number; ok: boolean };
|
|
const caches = new WeakMap<Ctx, { cached: Cached | null; pending: Promise<Church | null> | null }>();
|
|
|
|
async function fetchProfile(accountsUrl: string): Promise<Church> {
|
|
const res = await fetch(`${accountsUrl.replace(/\/+$/, '')}${CHURCH_PATHS.profile}`, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(TIMEOUT_MS) });
|
|
if (!res.ok) throw new Error(`Accounts answered ${res.status}`);
|
|
const parsed = churchSchema.safeParse(await res.json());
|
|
if (!parsed.success) throw new Error(`the profile isn't crosscode.church/1 (${parsed.error.issues[0]?.message ?? 'invalid'})`);
|
|
return parsed.data;
|
|
}
|
|
|
|
async function localProfile(ctx: Ctx): Promise<Church | null> {
|
|
const raw = await ctx.settings.json<unknown>('church_profile_local', null);
|
|
const parsed = raw ? churchSchema.safeParse(raw) : null;
|
|
return parsed?.success ? parsed.data : null;
|
|
}
|
|
|
|
/** The church's profile, or null when there's none yet. Never throws. */
|
|
export async function churchProfile(ctx: Ctx): Promise<Church | null> {
|
|
let link;
|
|
try {
|
|
link = await accountsLink(ctx);
|
|
} catch {
|
|
link = null;
|
|
}
|
|
if (!link) return localProfile(ctx).catch(() => null);
|
|
let state = caches.get(ctx);
|
|
if (!state) caches.set(ctx, (state = { cached: null, pending: null }));
|
|
const { cached } = state;
|
|
const url = link.url;
|
|
if (cached && cached.url === url && ctx.now().getTime() - cached.at < (cached.ok ? FRESH_MS : RETRY_MS)) return cached.church;
|
|
if (!state.pending) {
|
|
const last = cached?.url === url ? cached.church : null;
|
|
const s = state;
|
|
s.pending = fetchProfile(url)
|
|
.then((church) => {
|
|
s.cached = { url, church, at: ctx.now().getTime(), ok: true };
|
|
return church;
|
|
}, (e: Error) => {
|
|
console.warn('[church] profile not fetched from Accounts:', e.message);
|
|
s.cached = { url, church: last, at: ctx.now().getTime(), ok: false };
|
|
return last;
|
|
})
|
|
.finally(() => { s.pending = null; });
|
|
}
|
|
return state.pending;
|
|
}
|
|
|
|
/** `church.changed` from Accounts: fetched again next time (the last copy is shown meanwhile). */
|
|
export function churchChanged(ctx: Ctx): void {
|
|
const state = caches.get(ctx);
|
|
if (state?.cached) state.cached = { ...state.cached, at: 0 };
|
|
}
|