Fix frontend build hanging when the backend is unreachable
Server-side fetches now fail fast when no API base URL is configured, and the home and events pages build with an empty list if /api/events can't be reached during next build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,10 @@ and this project follows [Semantic Versioning](https://semver.org/).
|
|||||||
|
|
||||||
## [Unreleased]
|
## [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
|
### 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.
|
- 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.
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Metadata } from "next";
|
|||||||
import { Navbar } from "@/components/layout/Navbar";
|
import { Navbar } from "@/components/layout/Navbar";
|
||||||
import { Footer } from "@/components/layout/Footer";
|
import { Footer } from "@/components/layout/Footer";
|
||||||
import { EventCard } from "@/components/events/EventCard";
|
import { EventCard } from "@/components/events/EventCard";
|
||||||
import { apiFetch } from "@/lib/api";
|
import { apiFetchListForPrerender } from "@/lib/api";
|
||||||
import { Calendar, CalendarX } from "lucide-react";
|
import { Calendar, CalendarX } from "lucide-react";
|
||||||
|
|
||||||
export const revalidate = 60;
|
export const revalidate = 60;
|
||||||
@@ -27,7 +27,7 @@ type Event = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default async function EventsPage() {
|
export default async function EventsPage() {
|
||||||
const events = await apiFetch<Event[]>("/api/events", { nextOptions: { next: { revalidate: 60 } } });
|
const events = await apiFetchListForPrerender<Event>("/api/events", { nextOptions: { next: { revalidate: 60 } } });
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const upcoming = (events || []).filter(e => {
|
const upcoming = (events || []).filter(e => {
|
||||||
if (e.cashupStatus === "closed") return false;
|
if (e.cashupStatus === "closed") return false;
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ const appName = process.env.NEXT_PUBLIC_APP_NAME || 'Cross Code';
|
|||||||
* to fail the whole page render over.
|
* to fail the whole page render over.
|
||||||
*/
|
*/
|
||||||
async function getServerSettings(): Promise<SiteSettings> {
|
async function getServerSettings(): Promise<SiteSettings> {
|
||||||
|
// 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 {
|
try {
|
||||||
const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } });
|
const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } });
|
||||||
if (!res.ok) return {};
|
if (!res.ok) return {};
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type Event = {
|
|||||||
picture?: string;
|
picture?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
import { apiFetch } from "@/lib/api";
|
import { apiFetchListForPrerender } from "@/lib/api";
|
||||||
|
|
||||||
const FEATURES = [
|
const FEATURES = [
|
||||||
{ icon: CalendarCheck, title: "Meaningful Events", description: "Events designed to inspire, encourage and strengthen your faith." },
|
{ 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<string> {
|
async function getOrgName(): Promise<string> {
|
||||||
|
if (!API_BASE) return appName;
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } });
|
const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } });
|
||||||
if (!res.ok) return appName;
|
if (!res.ok) return appName;
|
||||||
@@ -40,7 +41,7 @@ async function getOrgName(): Promise<string> {
|
|||||||
|
|
||||||
export default async function HomePage() {
|
export default async function HomePage() {
|
||||||
const [events, displayName] = await Promise.all([
|
const [events, displayName] = await Promise.all([
|
||||||
apiFetch<Event[]>("/api/events", { nextOptions: { next: { revalidate: 60 } } }),
|
apiFetchListForPrerender<Event>("/api/events", { nextOptions: { next: { revalidate: 60 } } }),
|
||||||
getOrgName(),
|
getOrgName(),
|
||||||
]);
|
]);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|||||||
@@ -84,6 +84,13 @@ export async function apiFetch<T = any>(path: string, options: ApiOptions = {}):
|
|||||||
|
|
||||||
const url = path.startsWith("http") ? path : `${API_BASE}${path}`;
|
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<string, string> = { ...headers };
|
const mergedHeaders: Record<string, string> = { ...headers };
|
||||||
if (!(body instanceof FormData)) {
|
if (!(body instanceof FormData)) {
|
||||||
mergedHeaders["Content-Type"] = "application/json";
|
mergedHeaders["Content-Type"] = "application/json";
|
||||||
@@ -120,6 +127,20 @@ export async function apiFetch<T = any>(path: string, options: ApiOptions = {}):
|
|||||||
return res.text();
|
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<T>(path: string, options: ApiOptions = {}): Promise<T[]> {
|
||||||
|
try {
|
||||||
|
return (await apiFetch<T[]>(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
|
// Several list endpoints (/api/users, /api/payments) are paginated and cap
|
||||||
// `limit` at 200 server-side, so a single request can silently miss records
|
// `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.
|
// beyond the first page. This walks every page and returns the full list.
|
||||||
|
|||||||
Reference in New Issue
Block a user