-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
32 lines (24 loc) · 901 Bytes
/
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
import { NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
// 1st approach
// return NextResponse.redirect(new URL("/", request.url))
// // 2nd approach
// if (request.nextUrl.pathname === "/profile"){
// // return NextResponse.redirect(new URL("/hello", request.url));
// // We can also choose to rewrite the url
// return NextResponse.rewrite(new URL("/hello", request.url));
// }
// working with cookies & headers in middleware
const response = NextResponse.next();
const themePreference = request.cookies.get("theme");
if (!themePreference) {
response.cookies.set("theme", "dark");
}
// working with headers
response.headers.set("custom-header", "custom-value");
return response;
}
// 1st approach
// export const config = {
// matcher: "/profile"
// }