forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
74 lines (61 loc) · 2.04 KB
/
middleware.js
File metadata and controls
74 lines (61 loc) · 2.04 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
import { NextResponse } from 'next/server'
// we use this trick to fool static analysis at build time, so we can build a
// middleware that will return a body at run time, and check it is disallowed.
class MyResponse extends Response {}
export async function middleware(request, ev) {
// eslint-disable-next-line no-undef
const { readable, writable } = new TransformStream()
const url = request.nextUrl
const writer = writable.getWriter()
const encoder = new TextEncoder()
const next = NextResponse.next()
// this is needed for tests to get the BUILD_ID
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) {
return NextResponse.next()
}
// Header based on query param
if (url.searchParams.get('nested-header') === 'true') {
next.headers.set('x-nested-header', 'valid')
}
// Ensure deep can append to this value
if (url.searchParams.get('append-me') === 'true') {
next.headers.append('x-append-me', 'top')
}
// Ensure deep can append to this value
if (url.searchParams.get('cookie-me') === 'true') {
next.headers.append('set-cookie', 'bar=chocochip')
}
// Sends a header
if (url.pathname === '/header') {
next.headers.set('x-first-header', 'valid')
return next
}
if (url.pathname === '/two-cookies') {
const headers = new Headers()
headers.append('set-cookie', 'foo=chocochip')
headers.append('set-cookie', 'bar=chocochip')
return new Response(null, { headers })
}
// Streams a basic response
if (url.pathname === '/stream-a-response') {
ev.waitUntil(
(async () => {
writer.write(encoder.encode('this is a streamed '))
writer.write(encoder.encode('response '))
writer.close()
})()
)
return new MyResponse(readable)
}
if (url.pathname === '/bad-status') {
return new Response(null, {
headers: { 'WWW-Authenticate': 'Basic realm="Secure Area"' },
status: 401,
})
}
// Sends response
if (url.pathname === '/send-response') {
return new MyResponse(JSON.stringify({ message: 'hi!' }))
}
return next
}