-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
107 lines (87 loc) · 3.45 KB
/
Copy pathmiddleware.ts
File metadata and controls
107 lines (87 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { type NextRequest, NextResponse } from 'next/server';
import { rootDomain } from '@/lib/utils';
import { getSubdomainByCustomDomain } from '@/lib/subdomains';
import { isSubdomain } from '@/lib/domain-utils';
function extractSubdomain(request: NextRequest): string | null {
const url = request.url;
const host = request.headers.get('host') || '';
const hostname = host.split(':')[0];
// Local development environment
if (url.includes('localhost') || url.includes('127.0.0.1')) {
// Try to extract subdomain from the full URL
const fullUrlMatch = url.match(/http:\/\/([^.]+)\.localhost/);
if (fullUrlMatch && fullUrlMatch[1]) {
return fullUrlMatch[1];
}
// Fallback to host header approach
if (hostname.includes('.localhost')) {
return hostname.split('.')[0];
}
return null;
}
// Production environment
const rootDomainFormatted = rootDomain.split(':')[0];
// Handle Cloudflare Workers preview URLs (tenant---branch-name.pages.dev)
if (hostname.includes('---') && hostname.endsWith('.pages.dev')) {
const parts = hostname.split('---');
return parts.length > 0 ? parts[0] : null;
}
// Regular subdomain detection
const isSubdomain =
hostname !== rootDomainFormatted &&
hostname !== `www.${rootDomainFormatted}` &&
hostname.endsWith(`.${rootDomainFormatted}`);
return isSubdomain ? hostname.replace(`.${rootDomainFormatted}`, '') : null;
}
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const host = request.headers.get('host') || '';
const hostname = host.split(':')[0];
const subdomain = extractSubdomain(request);
// Avoid rewriting already rewritten paths to prevent loops
if (pathname.startsWith('/s/')) {
return NextResponse.next();
}
if (subdomain) {
// Block access to admin page from subdomains
if (pathname.startsWith('/admin')) {
return NextResponse.redirect(new URL('/', request.url));
}
// Rewrite all paths on a subdomain to the subdomain page
return NextResponse.rewrite(new URL(`/s/${subdomain}${pathname}`, request.url));
} else {
// Check if this is a custom domain
const rootDomainFormatted = rootDomain.split(':')[0];
// Skip if it's the root domain or www subdomain
if (hostname === rootDomainFormatted || hostname === `www.${rootDomainFormatted}`) {
return NextResponse.next();
}
// Skip localhost and preview URLs
if (hostname.includes('localhost') || hostname.includes('127.0.0.1') || hostname.includes('pages.dev') || hostname.includes('workers.dev')) {
return NextResponse.next();
}
// Check if this hostname is a custom domain
const customDomainData = await getSubdomainByCustomDomain(hostname);
if (customDomainData) {
// Block access to admin page from custom domains
if (pathname.startsWith('/admin')) {
return NextResponse.redirect(new URL('/', request.url));
}
// Rewrite all paths on a custom domain to the associated subdomain page
return NextResponse.rewrite(new URL(`/s/${customDomainData.subdomain}${pathname}`, request.url));
}
}
// On the root domain, allow normal access
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
* 3. all root files inside /public (e.g. /favicon.ico)
*/
'/((?!api|_next|[\\w-]+\\.\\w+).*)'
]
};