Files
crosscode-platform/apps/website/frontend/src/site/load.ts
T
joshuaandClaude Opus 5.5 085265392e feat(website): church details, settings, audit log, search engine data, safer editing
Church details saved through Accounts' church.update (live at once).
Settings: Search Console and Bing codes, Coming soon line, menu order.
Audit log and recent edits. A made share image, Event / VideoObject /
PodcastEpisode / BreadcrumbList structured data, permanent redirects
from old page addresses. Saves refused when someone else saved in
between; Ministry leaders' events; templates previewed with the
church's own content before choosing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:30:27 +02:00

64 lines
2.9 KiB
TypeScript

import { cookies } from "next/headers";
import { cache } from "react";
import type { Image, Published, SiteContent, Template } from "./types";
const api = (process.env.WEBSITE_API_URL || "http://127.0.0.1:5300").replace(/\/+$/, "");
const FALLBACK: SiteContent = {
mode: "coming-soon",
template: "heritage",
comingSoonText: "Our new website is on its way.",
church: null,
palette: { brand: "31 78 121", brandDark: "25 62 97", onBrand: "255 255 255", brandText: "31 78 121", brandTint: "241 244 248", accent: "87 113 148", onAccent: "255 255 255", notes: [] },
style: { fontPairing: "classic", corners: "sharp" },
};
/**
* The site as visitors see it: one fetch per request (React `cache`), kept by Next for
* WEBSITE_CONTENT_CACHE_SECONDS (30; tag `site-content`, which Publish clears at once through
* app/revalidate). While the backend can't answer: Coming soon, never an error page.
*/
export const loadContent = cache(async (): Promise<SiteContent> => {
// Previewing (§4): the working copy, if the backend agrees this is an admin or a live share link.
const jar = await cookies();
const preview = jar.get("ccw_preview")?.value;
if (preview) {
try {
const session = jar.get("ccw_session")?.value;
const res = await fetch(`${api}/api/v1/site/preview`, {
cache: "no-store",
headers: {
...(session ? { Cookie: `ccw_session=${encodeURIComponent(session)}` } : {}),
...(preview !== "admin" ? { "x-preview-token": preview } : {}),
...(jar.get("ccw_preview_template")?.value ? { "x-preview-template": jar.get("ccw_preview_template")!.value } : {}),
},
});
if (res.ok) return (await res.json()) as SiteContent;
} catch {
// Falls back to the live site.
}
}
try {
// Not kept in development, or when set to 0 (the e2e run changes content behind Next's back).
const seconds = process.env.NODE_ENV === "production" ? Number(process.env.WEBSITE_CONTENT_CACHE_SECONDS ?? 30) : 0;
const res = await fetch(`${api}/api/v1/site/content`, seconds > 0 ? { next: { revalidate: seconds, tags: ["site-content"] } } : { cache: "no-store" });
if (res.ok) return (await res.json()) as SiteContent;
} catch {
// Starting or down.
}
return FALLBACK;
});
export async function loadPublished(): Promise<Published | null> {
const c = await loadContent();
return c.mode === "published" ? c : null;
}
/** An image from a section's data (a media id), or null. */
export const media = (site: Published, id: unknown): Image | null => (typeof id === "string" ? site.snapshot.media[id] ?? null : null);
/** Per-template class names: `t(template, { heritage: "…", bold: "…" }, "fallback")`. */
export const t = (template: Template, by: Partial<Record<Template, string>>, otherwise = "") => by[template] ?? otherwise;
export const str = (v: unknown): string | null => (typeof v === "string" && v.trim() ? v : null);