-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmiddleware.ts
119 lines (101 loc) · 3.48 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
import { NextResponse } from 'next/server'
import { MiddlewareRequest, NextRequest } from '@netlify/next'
export async function middleware(req: NextRequest) {
let response
const { pathname } = req.nextUrl
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 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/hello')) {
// Add a header to the request
req.headers.set('x-hello', 'world')
return request.next()
}
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('/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('/shows')) {
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: '/shows/((?!99|88).*)' },
{
source: '/conditional',
has: [
{
type: 'header',
key: 'x-my-header',
value: 'my-value',
},
],
},
],
}