Next.js & App Router (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Multi-Tenant Architecture in Next.js: Wildcard Subdomain Routing.
senior LevelCode ExampleRoutingSystem Design
In Simple Words:
Building platforms like Shopify or Notion where each customer has subdomain.company.com:
- Configure wildcards in
middleware.ts:typescriptexport function middleware(req: NextRequest) { const hostname = req.headers.get('host')!; const subdomain = hostname.split('.')[0]; // Rewrites internally: tenant.myapp.com/products -> app/[subdomain]/products return NextResponse.rewrite(new URL(`/${subdomain}${req.nextUrl.pathname}`, req.url)); }