Created
October 26, 2023 14:05
-
-
Save luanlmd/5430733273934bda710f5b9bc3d58e56 to your computer and use it in GitHub Desktop.
nextjs middleware to mess with headers and cookies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextRequest, NextResponse } from 'next/server'; | |
export default function middleware(request: NextRequest) { | |
if (request.nextUrl.pathname.length === 11) { | |
const cookieKey = 'abc123'; | |
console.log('aqui'); | |
const requestHeaders = new Headers(request.headers); | |
requestHeaders.set('x-hello-from-middleware1', 'hello'); | |
const response = NextResponse.next({ | |
request: { | |
headers: requestHeaders, | |
}, | |
}); | |
response.cookies.set(cookieKey, 'hi'); | |
response.headers.set('x-hello-from-middleware2', 'hello'); | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment