settlekit/apps/checkout/middleware.ts
caelum0x 1d92fa23f2 feat: branded hosted storefront per merchant (G12)
Merchants can publish a store (slug, title, tagline, https logo, accent
colour, search description, optional custom domain) through settings or
the dashboard. The checkout app serves it at /store/<slug> with SEO and
Open Graph metadata, listing active products (invoice products hidden)
that each open their payment link, and a middleware rewrites '/' on a
merchant's custom domain to its store when CHECKOUT_PRIMARY_HOSTS is set.
Slugs and domains are unique; public data at /v1/public/stores. Carts of
several products stay out of scope because hosted checkout fulfils one
product per session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 06:32:06 +03:00

22 lines
1.0 KiB
TypeScript

// Custom storefront domains. When CHECKOUT_PRIMARY_HOSTS lists this
// checkout's own hosts, a request for "/" on any OTHER host (a merchant's
// domain pointed here by the host, e.g. Railyard or Caddy) is rewritten to
// that domain's storefront. Every other path is untouched, so payment links,
// checkouts and invoices keep working on custom domains too.
import { NextResponse, type NextRequest } from "next/server";
export function middleware(req: NextRequest): NextResponse {
const primary = (process.env.CHECKOUT_PRIMARY_HOSTS ?? "")
.split(",")
.map((h) => h.trim().toLowerCase())
.filter(Boolean);
if (primary.length === 0 || req.nextUrl.pathname !== "/") return NextResponse.next();
const host = (req.headers.get("host") ?? "").toLowerCase().replace(/:\d+$/, "");
if (!host || primary.includes(host)) return NextResponse.next();
const url = req.nextUrl.clone();
url.pathname = `/store/by-domain/${encodeURIComponent(host)}`;
return NextResponse.rewrite(url);
}
export const config = { matcher: ["/"] };