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:
@@ -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<Event[]>("/api/events", { nextOptions: { next: { revalidate: 60 } } });
|
||||
const events = await apiFetchListForPrerender<Event>("/api/events", { nextOptions: { next: { revalidate: 60 } } });
|
||||
const now = Date.now();
|
||||
const upcoming = (events || []).filter(e => {
|
||||
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.
|
||||
*/
|
||||
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 {
|
||||
const res = await fetch(`${API_BASE}/api/settings`, { next: { revalidate: 60 } });
|
||||
if (!res.ok) return {};
|
||||
|
||||
@@ -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<string> {
|
||||
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<string> {
|
||||
|
||||
export default async function HomePage() {
|
||||
const [events, displayName] = await Promise.all([
|
||||
apiFetch<Event[]>("/api/events", { nextOptions: { next: { revalidate: 60 } } }),
|
||||
apiFetchListForPrerender<Event>("/api/events", { nextOptions: { next: { revalidate: 60 } } }),
|
||||
getOrgName(),
|
||||
]);
|
||||
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}`;
|
||||
|
||||
// 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 };
|
||||
if (!(body instanceof FormData)) {
|
||||
mergedHeaders["Content-Type"] = "application/json";
|
||||
@@ -120,6 +127,20 @@ export async function apiFetch<T = any>(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<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
|
||||
// `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.
|
||||
|
||||
Reference in New Issue
Block a user