-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.ts
63 lines (55 loc) · 1.89 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
52
53
54
55
56
57
58
59
60
61
62
63
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export default function middleware(req: NextRequest) {
const url = req.nextUrl.clone()
// Get pathname of request (e.g. /blog-slug)
const { pathname } = req.nextUrl
// Get hostname of request (e.g. demo.vercel.pub)
const hostname = req.headers.get('host')
if (!hostname) {
return new Response(null, {
status: 400,
statusText: 'No hostname found in request headers',
})
}
let currentHost
if (hostname.includes('staging.')) {
currentHost = hostname.replace(`.staging.resilienceweb.org.uk`, '')
} else {
currentHost =
process.env.NODE_ENV === 'production' && process.env.VERCEL === '1'
? hostname
.replace('.cambridgeresilienceweb.org.uk', '')
.replace('.resilienceweb.org.uk', '')
: hostname.replace(`.localhost:3000`, '')
}
if (hostname === 'transition') {
return NextResponse.rewrite(new URL('/transition', req.url))
}
if (!pathname.includes('.') && !pathname.startsWith('/api')) {
if (
hostname === 'localhost:3000' ||
hostname === 'cambridgeresilienceweb.org.uk' ||
hostname === 'resilienceweb.org.uk' ||
hostname === 'staging.resilienceweb.org.uk' ||
(process.env.VERCEL_ENV === 'preview' &&
hostname === process.env.VERCEL_URL)
) {
return NextResponse.rewrite(url)
}
return NextResponse.rewrite(new URL(`/${currentHost}${pathname}`, req.url))
}
}
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - ph-ingest (PostHog)
*/
export const config = {
matcher: [
'/((?!api|admin|_next/static|_next/image|favicon.svg|favicon.ico|ph-ingest).*)',
],
}