-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
38 lines (29 loc) · 1 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
console.log('Hello from middleware');
if (['/config-node', '/config-edge', '/config-gssp'].includes(request.nextUrl.pathname)) {
return NextResponse.next();
}
const rewriteDestination = request.nextUrl.searchParams.get('rewrite_to');
let url: URL;
try {
url = new URL(rewriteDestination || '');
} catch (_) {
url = new URL([request.nextUrl.protocol,
'//',
request.nextUrl.hostname,
':',
request.nextUrl.port,
rewriteDestination || request.nextUrl.pathname,
'?foo=bar&',
request.nextUrl.searchParams.toString(),
].join(''));
}
const response = NextResponse.rewrite(url);
// Set a header
response.headers.set('my-header', 'baz');
// Set a cookie
response.cookies.set('my-cookie', 'wyz');
return response;
}