Overview
Each seller gets their own branded subdomain on the platform:How It Works
1. Middleware Detection
The middleware runs on every request and extracts the subdomain:middleware.ts
2. Database Lookup
Supabase query to find store by subdomain:3. Store Context
The store data is passed to pages via URL params or React Context:app/layout.tsx
Local Development
Hosts File Setup
To test subdomains locally, add entries to/etc/hosts:
Access Stores
http://teststore.localhost:3000→ Test Storehttp://myshop.localhost:3000→ My Shophttp://demo.localhost:3000→ Demo Store
Note: Use
.localhost (not just localhost) for subdomain routing to work locally.Production Setup
DNS Configuration
Wildcard DNS Record:Cloudflare Pages
Cloudflare automatically supports wildcard subdomains with Pages:- Deploy to Cloudflare Pages
- Add custom domain:
mypopup.shop - Wildcard
*.mypopup.shopworks automatically ✅
Subdomain Validation
Rules
Subdomains must:- Be 3-30 characters long
- Contain only lowercase letters, numbers, and hyphens
- Start with a letter or number
- Not start or end with a hyphen
- Be unique across all stores
Reserved Subdomains
These are not available for stores:www,api,admin,appblog,docs,help,supportmail,email,smtp,ftpcdn,static,assetstest,staging,dev
Multi-Tenant Data Isolation
Database Queries
All queries must filter bystore_id:
Row-Level Security (RLS)
Supabase RLS policies enforce data isolation:Performance Optimization
Caching Strategy
Edge Middleware
Middleware runs on the edge (Cloudflare/Vercel), making subdomain detection blazing fast:Troubleshooting
Subdomain not working locally
Subdomain not working locally
Make sure:
- You added the subdomain to
/etc/hosts - You’re using
.localhost(e.g.,test.localhost:3000) - Middleware is enabled in
middleware.ts - Clear browser cache
404 on subdomain in production
404 on subdomain in production
Check:
- Wildcard DNS record is set (
*.mypopup.shop) - Store exists in database with that subdomain
- Store status is
active - Cloudflare proxy is enabled (orange cloud)
Wrong store data showing
Wrong store data showing
This is a critical bug - ensure:
- All database queries filter by
store_id - RLS policies are enabled
- Store context is passed correctly
- No caching issues (clear Redis/cache)
Example Flow
Next Steps
Learn how payments are processed per store
