-
Notifications
You must be signed in to change notification settings - Fork 280
/
middleware.ts
51 lines (46 loc) · 1.46 KB
/
middleware.ts
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
import { NextRequest, NextResponse } from 'next/server'
export const config = {
matcher: [
/*
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
* 3. /examples (inside /public)
* 4. all root files inside /public (e.g. /favicon.ico)
*/
'/((?!api/|__sentry/|_next/|_static/|[\\w-]+\\.\\w+).*)',
],
}
export function middleware(req: NextRequest) {
const url = req.nextUrl
const hostname = req.headers.get('host')
const path = url.pathname
if (!hostname) return
const currentHost =
process.env.NODE_ENV === 'production' && process.env.VERCEL === '1'
? hostname
.replace(`.${process.env.NEXT_PUBLIC_APP_PUBLIC_DOMAIN}`, '')
.replace(`.${process.env.NEXT_PUBLIC_APP_CUSTOM_DOMAIN}`, '')
: hostname.replace(`.localhost:3000`, '')
if (
!currentHost ||
currentHost === process.env.NEXT_PUBLIC_APP_PUBLIC_DOMAIN ||
currentHost === `www`
) {
if (path == '/signin' || path == '/signup') {
return NextResponse.redirect(
`${url.protocol}//${process.env.NEXT_PUBLIC_APP_DASHBOARD_DOMAIN}.${process.env.NEXT_PUBLIC_APP_PUBLIC_DOMAIN}${path}`
)
}
return
}
if (
currentHost &&
currentHost === process.env.NEXT_PUBLIC_APP_DASHBOARD_DOMAIN
) {
return NextResponse.rewrite(new URL(`/__app${path}${url.search}`, req.url))
}
return NextResponse.rewrite(
new URL(`/sites/${currentHost}${path}${url.search}`, req.url)
)
}