Skip to content

Instantly share code, notes, and snippets.

@luanlmd
Created October 26, 2023 14:05
Show Gist options
  • Save luanlmd/5430733273934bda710f5b9bc3d58e56 to your computer and use it in GitHub Desktop.
Save luanlmd/5430733273934bda710f5b9bc3d58e56 to your computer and use it in GitHub Desktop.
nextjs middleware to mess with headers and cookies
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