Skip to main content
InterviewHubPRO
Section Overview
Q36

Question 36 of 50

Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

72%
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:
    typescript
    export 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));
    }