Picture the failure mode that worries me most on a service business site. A homeowner fills out the contact form, clicks Submit, and sees a green "Thanks, we will be in touch." Behind that message, the handler called a third party email or form service, the call failed or got throttled, and nobody logged it. The owner sees an empty inbox and concludes the site is dead. The visitor waits for a callback that is never coming.

That is the worst kind of bug. Nothing errors. The visitor believes they are in the queue. The owner believes the site is dead. And the lead, which on a service site can represent a job worth thousands of dollars, is gone with no trace.

If you build sites for service businesses, the contact form is the single most valuable piece of code you ship. Here is how to build the handler so a lead cannot vanish silently, using a Next.js route handler and about sixty lines of TypeScript.

The rule: store first, notify second

Most form handlers do one thing: send an email. If the email provider is down, slow, or throttled, the lead is lost. The fix is a change in order, not a change in vendor.

The handler should write the submission to a durable store you control, and only then try to notify anyone. The HTTP 200 the browser sees should mean "this is saved," never "we tried to email someone." Notification is a side effect that is allowed to fail. Storage is not.

Here is the shape of that handler in the App Router.

// app/api/lead/route.ts import { NextResponse } from "next/server"; import { z } from "zod"; import { createClient } from "@supabase/supabase-js"; import { sendLeadEmail } from "@/lib/email"; const LeadSchema = z.object({ name: z.string().trim().min(2).max(120), phone: z.string().trim().min(7).max(30), email: z.string().trim().email().optional().or(z.literal("")), message: z.string().trim().min(5).max(4000), service: z.string().trim().max(80).optional(), // honeypot: real users never see or fill this field company_website: z.string().max(0).optional(), }); const supabase = createClient( process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY! ); export async function POST(req: Request) { const body = await req.json().catch(() => null); const parsed = LeadSchema.safeParse(body); if (!parsed.success) { return NextResponse.json( { ok: false, error: "Please check the highlighted fields." }, { status: 400 } ); } // Bots that fill the honeypot get a quiet 200 and nothing else. if (parsed.data.company_website) { return NextResponse.json({ ok: true }); } const lead = { ...parsed.data, source_path: req.headers.get("referer") ?? null, user_agent: req.headers.get("user-agent") ?? null, created_at: new Date().toISOString(), notified_at: null as string | null, }; // 1. Store. If this fails, the visitor must see an error. const { data, error } = await supabase .from("leads") .insert(lead) .select("id") .single(); if (error) { console.error("lead insert failed", error); return NextResponse.json( { ok: false, error: "Something went wrong. Please call us directly." }, { status: 500 } ); } // 2. Notify. Failure here is logged, not surfaced. try { await sendLeadEmail({ id: data.id, ...lead }); await supabase .from("leads") .update({ notified_at: new Date().toISOString() }) .eq("id", data.id); } catch (err) { console.error("lead email failed", data.id, err); } return NextResponse.json({ ok: true, id: data.id }); } 

Three things are doing the real work here.

The schema rejects junk before it touches your database, and it does so with messages a human can act on. "Please check the highlighted fields" beats a generic failure, and a 400 tells your client component exactly which path to take.

The honeypot is a hidden field named like something a bot wants to fill. Real visitors never see it, so a value there means the request is automated. Returning a cheerful 200 is deliberate: bots that get errors try harder.

The insert happens before the email, and notified_at stays null until the email succeeds. That one column is what makes the next step possible.

The visitor should never see a fake success

On the client side, resist the urge to show a thank you message optimistically. Wait for the response and branch on it.

// components/LeadForm.tsx "use client"; import { useState } from "react"; export function LeadForm() { const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle"); const [error, setError] = useState<string | null>(null); async function onSubmit(e: React.FormEvent<HTMLFormElement>) { e.preventDefault(); setStatus("sending"); const form = Object.fromEntries(new FormData(e.currentTarget)); const res = await fetch("/api/lead", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }).catch(() => null); const json = await res?.json().catch(() => null); if (res?.ok && json?.ok) { setStatus("sent"); return; } setStatus("error"); setError(json?.error ?? "Something went wrong. Please call us directly."); } // render fields, the hidden company_website input, and the status message } 

Put the business phone number in that fallback string. When the form fails, the visitor still has a way to reach the business in the same breath. For a roofer or an HVAC company, that single line recovers calls that a bare "error" would have lost.

Form design matters as much as the handler. We wrote about why a long or confusing form quietly costs more than a slow page, and the short version is that every field you add is a decision the visitor has to make while standing in a flooded kitchen. Ask for a name, a phone number, and what is wrong. Everything else can wait for the callback.

Reconcile every morning

The notified_at column turns a silent failure into a visible one. Add a scheduled route that looks for leads that were saved but never delivered, and retries them.

// app/api/cron/retry-leads/route.ts import { NextResponse } from "next/server"; import { createClient } from "@supabase/supabase-js"; import { sendLeadEmail } from "@/lib/email"; export async function GET(req: Request) { if (req.headers.get("authorization") !== `Bearer ${process.env.CRON_SECRET}`) { return new NextResponse("Unauthorized", { status: 401 }); } const supabase = createClient( process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY! ); const { data: stuck } = await supabase .from("leads") .select("*") .is("notified_at", null) .order("created_at", { ascending: true }) .limit(50); let retried = 0; for (const lead of stuck ?? []) { try { await sendLeadEmail(lead); await supabase .from("leads") .update({ notified_at: new Date().toISOString() }) .eq("id", lead.id); retried++; } catch (err) { console.error("retry failed", lead.id, err); } } return NextResponse.json({ checked: stuck?.length ?? 0, retried }); } 

Wire it to a daily schedule in vercel.json and you have a system where a lead can be delayed by a provider outage but cannot be lost by one.

{ "crons": [{ "path": "/api/cron/retry-leads", "schedule": "0 14 * * *" }] } 

The same table gives the owner something they have never had: a list of every person who asked for help, with a timestamp, independent of whose inbox it landed in. That matters because delivery is only the first handoff. Once the email arrives, someone still has to call back, and that is where most service leads actually stall. The table makes the gap measurable.

Where this fits in a service business build

None of this is exotic. A route handler, a schema, a table, a cron. What makes it unusual is treating the form as infrastructure instead of as a widget you paste in from a third party dashboard.

When we build a website for a service business, this handler is one of the first files in the repo, before the hero section and before the color palette. The site exists to produce that row in the leads table. Everything else is decoration around it.

If you are inheriting a service site and want a quick audit, open the form handler and ask one question: what happens to the submission if the email call throws? If the answer is "the visitor sees a thank you anyway," you have found the leak.

Level Up Digital Marketing Group builds websites and search systems for San Diego service businesses. More at levelupdigitalmarketinggroup.com.