Skip to content

Commit e54fec6

Browse files
committed
chore: update example
1 parent 3854370 commit e54fec6

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

demos/middleware/middleware.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import type { NextRequest } from 'next/server'
33

44
import { MiddlewareRequest } from '@netlify/plugin-nextjs/middleware'
55

6-
export async function middleware(request: NextRequest) {
6+
export async function middleware(req: NextRequest) {
77
let response
88
const {
99
nextUrl: { pathname },
10-
} = request
10+
} = req
11+
12+
const request = new MiddlewareRequest(req)
1113

1214
if (pathname.startsWith('/static')) {
1315
// Unlike NextResponse.next(), this actually sends the request to the origin
14-
const res = await new MiddlewareRequest(request).next()
15-
const message = `This was static but has been transformed in ${request.geo.city}`
16+
const res = await request.next()
17+
const message = `This was static but has been transformed in ${req.geo.city}`
1618

1719
// Transform the response page data
1820
res.transformData((data) => {
@@ -37,14 +39,14 @@ export async function middleware(request: NextRequest) {
3739

3840
if (pathname.startsWith('/api/hello')) {
3941
// Add a header to the request
40-
request.headers.set('x-hello', 'world')
41-
return new MiddlewareRequest(request).next()
42+
req.headers.set('x-hello', 'world')
43+
return request.next()
4244
}
4345

4446
if (pathname.startsWith('/headers')) {
4547
// Add a header to the rewritten request
46-
request.headers.set('x-hello', 'world')
47-
return new MiddlewareRequest(request).rewrite('/api/hello')
48+
req.headers.set('x-hello', 'world')
49+
return request.rewrite('/api/hello')
4850
}
4951

5052
if (pathname.startsWith('/cookies')) {
@@ -55,15 +57,15 @@ export async function middleware(request: NextRequest) {
5557

5658
if (pathname.startsWith('/shows')) {
5759
if (pathname.startsWith('/shows/rewrite-absolute')) {
58-
response = NextResponse.rewrite(new URL('/shows/100', request.url))
60+
response = NextResponse.rewrite(new URL('/shows/100', req.url))
5961
response.headers.set('x-modified-in-rewrite', 'true')
6062
}
6163
if (pathname.startsWith('/shows/rewrite-external')) {
6264
response = NextResponse.rewrite('http://example.com/')
6365
response.headers.set('x-modified-in-rewrite', 'true')
6466
}
6567
if (pathname.startsWith('/shows/rewriteme')) {
66-
const url = request.nextUrl.clone()
68+
const url = req.nextUrl.clone()
6769
url.pathname = '/shows/100'
6870
response = NextResponse.rewrite(url)
6971
response.headers.set('x-modified-in-rewrite', 'true')

0 commit comments

Comments
 (0)