Question presented to candidate:
"We are building a store builder: every customer gets name.example.com, and paying customers can bring their own domain. Design the Next.js side: one app, many tenants. How do requests reach the right tenant, and what can go wrong?"
What a strong answer should cover:
- One deployment serves every tenant; wildcard DNS and TLS for
*.example.com, plus per-domain certificates for custom domains. proxy.tsreads theHostheader, resolves the tenant (subdomain or custom-domain lookup), and rewrites the URL to an internal route such as/s/[tenant]/...while the browser URL stays unchanged.- The tenant is part of the route path, so ISR and caches are keyed per tenant;
generateStaticParamscan prerender known tenants and new ones render on demand. - Isolation is enforced in the data layer (every query scoped by tenant), and the internal
/s/*paths must not be reachable directly. - Edge cases: unknown hosts, cached 404s for tenants created later, cookies scoped per host, Server Actions
allowedOriginsfor custom domains, and per-tenant metadata.
Clarifying questions expected:
- "Custom domains, or only subdomains?" β custom domains add certificate automation and a domain table.
- "How different are tenants: theme only, or features and data residency?" β that decides one app versus several deployments.
Code / implementation expected: Yes β a proxy.ts that rewrites by host, and the tenant route it rewrites to.