-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmiddleware.ts
156 lines (133 loc) · 4.45 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { NextResponse } from 'next/server'
import { MiddlewareRequest, NextRequest } from '@netlify/next'
// Next.js replaces this with a stub polyfill. This import is just to test that stub.
import pointlessFetch from 'isomorphic-unfetch'
export async function middleware(req: NextRequest) {
let response
const { pathname } = req.nextUrl
if (pathname.startsWith('/api/hello')) {
// Next 13 request header mutation functionality
const headers = new Headers(req.headers)
headers.set('x-hello', 'world')
return NextResponse.next({
request: {
headers
}
})
}
const request = new MiddlewareRequest(req)
if (pathname.startsWith('/static')) {
// Unlike NextResponse.next(), this actually sends the request to the origin
const res = await request.next()
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`
// Transform the response HTML and props
res.replaceText('p[id=message]', message)
res.setPageProp('message', message)
res.setPageProp('showAd', true)
res.headers.set('x-modified-edge', 'true')
res.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
return res
}
if (pathname.startsWith('/api/geo')) {
req.headers.set('x-geo-country', req.geo.country)
req.headers.set('x-geo-region', req.geo.region)
req.headers.set('x-geo-city', req.geo.city)
req.headers.set('x-geo-longitude', req.geo.longitude)
req.headers.set('x-geo-latitude', req.geo.latitude)
req.headers.set('x-geo-timezone', req.geo.timezone)
return request.next()
}
if (pathname.startsWith('/headers')) {
// Add a header to the rewritten request
req.headers.set('x-hello', 'world')
return request.rewrite('/api/hello')
}
if (pathname.startsWith('/cookies')) {
response = NextResponse.next()
response.cookies.set('netlifyCookie', 'true')
return response
}
if(pathname.startsWith('/matcher-cookie')) {
response = NextResponse.next()
response.cookies.set('missingCookie', 'true')
return response
}
if (pathname.startsWith('/conditional')) {
response = NextResponse.next()
response.headers.set('x-modified-edge', 'true')
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
return response
}
if (pathname.startsWith('/missing')) {
response = NextResponse.next()
response.headers.set('x-cookie-missing', 'true')
return response
}
if (pathname.startsWith('/shows')) {
if (pathname.startsWith('/shows/222')) {
response = NextResponse.next()
const res = await pointlessFetch('http://www.example.com/')
response.headers.set('x-example-server', res.headers.get('server'))
}
if (pathname.startsWith('/shows/rewrite-absolute')) {
response = NextResponse.rewrite(new URL('/shows/100', req.url))
response.headers.set('x-modified-in-rewrite', 'true')
}
if (pathname.startsWith('/shows/rewrite-external')) {
response = NextResponse.rewrite('http://example.com/')
response.headers.set('x-modified-in-rewrite', 'true')
}
if (pathname.startsWith('/shows/rewriteme')) {
const url = req.nextUrl.clone()
url.pathname = '/shows/100'
response = NextResponse.rewrite(url)
response.headers.set('x-modified-in-rewrite', 'true')
}
if (pathname.startsWith('/shows/redirectme')) {
const url = req.nextUrl.clone()
url.pathname = '/shows/100'
response = NextResponse.redirect(url)
}
if (pathname.startsWith('/shows/redirectexternal')) {
response = NextResponse.redirect('http://example.com/')
}
if (!response) {
response = NextResponse.next()
}
if (pathname.startsWith('/shows/static')) {
response.headers.set('x-middleware-date', new Date().toISOString())
}
response.headers.set('x-modified-edge', 'true')
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
return response
}
}
export const config = {
matcher: [
'/api/:all*',
'/headers',
{ source: '/static' },
{ source: '/cookies' },
{ source: '/matcher-cookie'},
{ source: '/shows/((?!99|88).*)' },
{
source: '/conditional',
has: [
{
type: 'header',
key: 'x-my-header',
value: 'my-value',
},
],
},
{
source: '/missing',
missing: [
{
type: 'cookie',
key: 'missingCookie',
}
],
},
],
}