The load balancer checks /api/health. After staff switch on maintenance, what do /api/health and /api/ready answer?
The shop runs on several servers behind a load balancer, which asks each server's /api/health every few seconds and stops sending it visitors when the answer is 503. /api/ready is the same check, written by a teammate who added await connection(). Before the database is taken down, staff call POST /api/maintenance. Cache Components is on, as create-next-app sets it up. What does next build print for the three Route Handlers, and what does each request get?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
app/page.tsxexport default function Home() {
return (
<main>
<h1>Leaf and Kettle</h1>
<p>Fresh tea, packed by hand.</p>
</main>
);
}
lib/maintenance.ts// Whether the shop is closed for maintenance. It lives on globalThis,
// so every route in the server reads the same switch.
const shop = globalThis as typeof globalThis & { inMaintenance?: boolean };
export function inMaintenance() {
return shop.inMaintenance === true;
}
export function startMaintenance() {
shop.inMaintenance = true;
}
app/api/health/route.tsimport { inMaintenance } from "@/lib/maintenance";
export async function GET() {
if (inMaintenance()) return Response.json({ status: "maintenance" }, { status: 503 });
return Response.json({ status: "ok" });
}
app/api/ready/route.tsimport { connection } from "next/server";
import { inMaintenance } from "@/lib/maintenance";
export async function GET() {
await connection();
if (inMaintenance()) return Response.json({ status: "maintenance" }, { status: 503 });
return Response.json({ status: "ok" });
}
app/api/maintenance/route.tsimport { startMaintenance } from "@/lib/maintenance";
// A stand-in for the staff tool. A real one checks who's asking first.
export async function POST() {
startMaintenance();
return new Response(null, { status: 204 });
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:
- You run
next buildandnext start. - The load balancer asks for
/api/healthand/api/ready. - Staff send
POST /api/maintenance. - The load balancer asks for
/api/healthand/api/readyagain.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…