This is a Next.js project bootstrapped with create-next-app.
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the file.
This project uses next/font to automatically optimize and load Geist, a new font family for Vercel.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.
Check out our Next.js deployment documentation for more details.
This middleware enables multi-tenant functionality in a Next.js application, handling routing for subdomains and custom domains, authentication, and tenant-specific content delivery.
Multi-tenant routing: Automatically routes requests to tenant-specific content based on subdomain or custom domain Authentication management: Protects routes with authentication and handles user sessions Custom domain support: Maps verified custom domains to their respective tenants Search engine control: Optional blocking of search engine bots based on configuration Path rewriting: Dynamically rewrites URLs to serve tenant-specific content Configuration
NEXT_PUBLIC_PROD_DOMAIN: Production domain (default: "bitbloom.se") NEXT_PUBLIC_LOCAL_DOMAIN: Local development domain (default: "localhost:3000") NEXT_PUBLIC_ALLOW_INDEXING: Set to 'false' to block search engine crawlers How It Works Request processing: Intercepts all incoming requests to the application Authentication check: Verifies user sessions for protected routes Domain resolution: Identifies the tenant based on subdomain or custom domain Path rewriting: Rewrites the request path to the tenant-specific content Public Paths
/login, /signup, /register, /auth/callback /_next/static /api/auth, /api/domains, /api/domains/verify Usage The middleware is automatically applied to all routes in your Next.js application. Key exported functions include:
middleware(req): Main middleware function that handles all requests addTenant(subdomain, tenantId): Helper function that returns appropriate host for a new tenant Database Integration The middleware interacts with a Supabase database to:
Verify custom domains Manage authentication sessions Example When a user visits customername.bitbloom.se, the middleware:
Extracts the subdomain (customername) Looks up the tenant in the database Rewrites the request to /tenant/customername/... Similarly for verified custom domains like example.com, it maps to the appropriate tenant configuration.
The middleware includes detailed console logging for debugging purposes, tracking:
Environment and domain information Session status Subdomain extraction Custom domain verification Path rewriting For more information, see the middleware.ts file.