diff --git a/apps/events/CHANGELOG.md b/apps/events/CHANGELOG.md index 7f30d0a..2b653fd 100644 --- a/apps/events/CHANGELOG.md +++ b/apps/events/CHANGELOG.md @@ -7,6 +7,10 @@ and this project follows [Semantic Versioning](https://semver.org/). ## [Unreleased] +### Fixed + +- The frontend production build (`next build`) no longer hangs or fails when the backend isn't reachable. With no `NEXT_PUBLIC_API_URL`/`API_URL` set, the root layout fetched a relative `/api/settings` URL that never settled during prerender, so every page timed out after 60 seconds; server-side fetches now fail fast when no API base is configured. The home page and the Events page also no longer fail the build when `/api/events` can't be reached — they build with an empty list and fill in on the first revalidation. At runtime a failed fetch still errors, so the last good page keeps being served. + ### Security - Updated dependencies to clear `npm audit` advisories: `express`, `multer`, `proxy-addr`, `qs` and others in the backend, `next` (15.5.27) and `sharp` (0.35.5) in the frontend, and `nodemailer` from 9 to 10 (requires Node.js 20 or newer). The root package now overrides `shell-quote` to a patched release, since every `concurrently` version pins a vulnerable one. Backend production dependencies now report no known vulnerabilities; the remaining advisories are in development tooling (`nodemon`, `jest`, `tailwindcss` 3) and have no non-breaking fix. diff --git a/apps/events/frontend/src/app/events/page.tsx b/apps/events/frontend/src/app/events/page.tsx index 9e378bb..8a5a0c3 100644 --- a/apps/events/frontend/src/app/events/page.tsx +++ b/apps/events/frontend/src/app/events/page.tsx @@ -2,7 +2,7 @@ import { Metadata } from "next"; import { Navbar } from "@/components/layout/Navbar"; import { Footer } from "@/components/layout/Footer"; import { EventCard } from "@/components/events/EventCard"; -import { apiFetch } from "@/lib/api"; +import { apiFetchListForPrerender } from "@/lib/api"; import { Calendar, CalendarX } from "lucide-react"; export const revalidate = 60; @@ -27,7 +27,7 @@ type Event = { }; export default async function EventsPage() { - const events = await apiFetch("/api/events", { nextOptions: { next: { revalidate: 60 } } }); + const events = await apiFetchListForPrerender("/api/events", { nextOptions: { next: { revalidate: 60 } } }); const now = Date.now(); const upcoming = (events || []).filter(e => { if (e.cashupStatus === "closed") return false; diff --git a/apps/events/frontend/src/app/layout.tsx b/apps/events/frontend/src/app/layout.tsx index 93e6590..fef9b3d 100644 --- a/apps/events/frontend/src/app/layout.tsx +++ b/apps/events/frontend/src/app/layout.tsx @@ -33,6 +33,9 @@ const appName = process.env.NEXT_PUBLIC_APP_NAME || 'Cross Code'; * to fail the whole page render over. */ async function getServerSettings(): Promise { + // No API base configured: a relative fetch never settles during prerender and + // would time out every page, so skip straight to the defaults. + if (!API_BASE) return {}; try { const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } }); if (!res.ok) return {}; diff --git a/apps/events/frontend/src/app/page.tsx b/apps/events/frontend/src/app/page.tsx index 0821e29..605005c 100644 --- a/apps/events/frontend/src/app/page.tsx +++ b/apps/events/frontend/src/app/page.tsx @@ -18,7 +18,7 @@ type Event = { picture?: string; }; -import { apiFetch } from "@/lib/api"; +import { apiFetchListForPrerender } from "@/lib/api"; const FEATURES = [ { icon: CalendarCheck, title: "Meaningful Events", description: "Events designed to inspire, encourage and strengthen your faith." }, @@ -28,6 +28,7 @@ const FEATURES = [ ]; async function getOrgName(): Promise { + if (!API_BASE) return appName; try { const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } }); if (!res.ok) return appName; @@ -40,7 +41,7 @@ async function getOrgName(): Promise { export default async function HomePage() { const [events, displayName] = await Promise.all([ - apiFetch("/api/events", { nextOptions: { next: { revalidate: 60 } } }), + apiFetchListForPrerender("/api/events", { nextOptions: { next: { revalidate: 60 } } }), getOrgName(), ]); const now = Date.now(); diff --git a/apps/events/frontend/src/lib/api.ts b/apps/events/frontend/src/lib/api.ts index 779baf6..79b110b 100644 --- a/apps/events/frontend/src/lib/api.ts +++ b/apps/events/frontend/src/lib/api.ts @@ -84,6 +84,13 @@ export async function apiFetch(path: string, options: ApiOptions = {}): const url = path.startsWith("http") ? path : `${API_BASE}${path}`; + // On the server a relative URL has no origin to resolve against, and Next's + // patched fetch never settles on one during prerender (the build then times out + // every page). Fail fast instead so callers can degrade. + if (typeof window === "undefined" && !url.startsWith("http")) { + throw new Error(`API base URL is not configured (set NEXT_PUBLIC_API_URL or API_URL); cannot fetch ${path}`); + } + const mergedHeaders: Record = { ...headers }; if (!(body instanceof FormData)) { mergedHeaders["Content-Type"] = "application/json"; @@ -120,6 +127,20 @@ export async function apiFetch(path: string, options: ApiOptions = {}): return res.text(); } +// For server components that prerender a public list: during `next build` the +// backend is often not running (or no API URL is set), so fall back to an empty +// list rather than failing the build — the page fills in on its first revalidate. +// At runtime the error still propagates, so ISR keeps serving the last good page +// instead of caching an empty one. +export async function apiFetchListForPrerender(path: string, options: ApiOptions = {}): Promise { + try { + return (await apiFetch(path, options)) || []; + } catch (err) { + if (process.env.NEXT_PHASE === "phase-production-build") return []; + throw err; + } +} + // Several list endpoints (/api/users, /api/payments) are paginated and cap // `limit` at 200 server-side, so a single request can silently miss records // beyond the first page. This walks every page and returns the full list.