-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmiddleware.js
91 lines (75 loc) · 2.35 KB
/
middleware.js
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
import { NextResponse } from 'next/server'
export async function middleware(request) {
const url = request.nextUrl
// this is needed for tests to get the BUILD_ID
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) {
return NextResponse.next()
}
if (url.pathname === '/old-home') {
if (url.searchParams.get('override') === 'external') {
return Response.redirect('https://example.vercel.sh')
} else {
url.pathname = '/new-home'
return Response.redirect(url)
}
}
if (url.searchParams.get('foo') === 'bar') {
url.pathname = '/new-home'
url.searchParams.delete('foo')
return Response.redirect(url)
}
// Chained redirects
if (url.pathname === '/redirect-me-alot') {
url.pathname = '/redirect-me-alot-2'
return Response.redirect(url)
}
if (url.pathname === '/redirect-me-alot-2') {
url.pathname = '/redirect-me-alot-3'
return Response.redirect(url)
}
if (url.pathname === '/redirect-me-alot-3') {
url.pathname = '/redirect-me-alot-4'
return Response.redirect(url)
}
if (url.pathname === '/redirect-me-alot-4') {
url.pathname = '/redirect-me-alot-5'
return Response.redirect(url)
}
if (url.pathname === '/redirect-me-alot-5') {
url.pathname = '/redirect-me-alot-6'
return Response.redirect(url)
}
if (url.pathname === '/redirect-me-alot-6') {
url.pathname = '/redirect-me-alot-7'
return Response.redirect(url)
}
if (url.pathname === '/redirect-me-alot-7') {
url.pathname = '/new-home'
return Response.redirect(url)
}
// Infinite loop
if (url.pathname === '/infinite-loop') {
url.pathname = '/infinite-loop-1'
return Response.redirect(url)
}
if (url.pathname === '/infinite-loop-1') {
url.pathname = '/infinite-loop'
return Response.redirect(url)
}
if (url.pathname === '/to') {
url.pathname = url.searchParams.get('pathname')
url.searchParams.delete('pathname')
return Response.redirect(url)
}
if (url.pathname === '/with-fragment') {
console.log(String(new URL('/new-home#fragment', url)))
return Response.redirect(new URL('/new-home#fragment', url))
}
if (url.pathname.includes('/json')) {
return NextResponse.json({
requestUrlPathname: new URL(request.url).pathname,
nextUrlPathname: request.nextUrl.pathname,
nextUrlLocale: request.nextUrl.locale,
})
}
}