-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathmiddleware.ts
More file actions
94 lines (74 loc) · 2.36 KB
/
middleware.ts
File metadata and controls
94 lines (74 loc) · 2.36 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
import { NextRequest, NextResponse } from 'next/server'
import { landingPageHostname, landingPageFramerHostname } from '@/app/hostnames'
import { replaceUrls } from '@/utils/replaceUrls'
export async function middleware(req: NextRequest): Promise<NextResponse> {
if (req.method !== 'GET') return NextResponse.next()
const url = new URL(req.nextUrl.toString())
url.protocol = 'https'
url.port = ''
if (url.pathname === '' || url.pathname === '/') {
if (process.env.NODE_ENV === 'production') {
url.hostname = landingPageHostname
} else {
return NextResponse.redirect(new URL('/dashboard', req.url))
}
}
if (url.pathname.startsWith('/terms')) {
url.hostname = landingPageHostname
}
if (url.pathname.startsWith('/privacy')) {
url.hostname = landingPageHostname
}
if (url.pathname.startsWith('/pricing')) {
url.hostname = landingPageHostname
}
if (url.pathname.startsWith('/cookbook')) {
url.hostname = landingPageHostname
}
if (url.pathname.startsWith('/changelog')) {
url.hostname = landingPageHostname
}
if (url.pathname.startsWith('/blog')) {
const segments = url.pathname.split('/')
url.hostname = landingPageHostname
if (segments[2] === 'category') {
url.pathname = segments.slice(2).join('/')
}
}
const headers = new Headers(req.headers)
// TODO: Not on the new landing page hosting yet
if (url.pathname.startsWith('/ai-agents')) {
url.hostname = landingPageFramerHostname
const res = await fetch(url.toString(), {
...req,
headers,
redirect: 'follow',
})
const htmlBody = await res.text()
// !!! NOTE: Replace has intentionally not completed quotes to catch the rest of the path !!!
const modifiedHtmlBody = replaceUrls(htmlBody, url.pathname, 'href="', '">')
return new NextResponse(modifiedHtmlBody, {
status: res.status,
statusText: res.statusText,
headers: res.headers,
url: req.url,
})
}
return NextResponse.rewrite(url.toString(), {
...req,
headers,
})
}
// We should probably filter all /, /blog and /changelog paths here and decide what to do with them in the middleware body.
export const config = {
matcher: [
'/',
'/blog/:path*',
'/changelog/:path*',
'/ai-agents/:path*',
'/privacy/:path*',
'/terms/:path*',
'/pricing/:path*',
'/cookbook/:path*',
],
}