NextJS NextRequest Context

I would like to understand a behavior in my NextJS application.

For each request a middleware is launched, which looks like this:

export async function middleware(request: NextRequest) {
   request.headers.set('Custom-Header', 'Custom-Value');
   ...
   return NextResponse.next();
}

Then a custom route is called (route handler):

export async function GET(request: NextRequest) {
   const customHeaderValue = request.headers.get('Custom-Header');
   console.log(customHeaderValue); // null
}

Could you explain to me why I don’t have access to the data defined in the middleware in the GET route ?

So I tried to set various values, but nothing works, and from what I understood the NextRequest object is the context of the current request.

Leave a Comment