I'm looking to include a custom header in each server request.
My current approach involves utilizing middleware in the following manner:
export async function middleware(req: NextRequest): Promise<NextResponse> {
req.headers.append('x-custom-header', '1337');
return NextResponse.next();
}
Upon checking with console.log(req.headers)
, I can confirm that the custom request header has been successfully added:
BaseHeaders [Headers] {
[Symbol(map)]: {
accept: [ '*/*' ],
'accept-encoding': [ 'gzip, deflate, br' ],
'accept-language': [ 'en-GB,en-US;q=0.9,en;q=0.8' ],
'cache-control': [ 'no-cache' ],
connection: [ 'keep-alive' ],
cookie: ...,
host: ...,
pragma: [ 'no-cache' ],
referer: ...,
...,
'x-custom-header': [ '1337' ]
}
}
However, despite adding the custom header, it does not reflect on the browser's request itself.
What could be causing this issue? Are there other methods available for modifying request headers in Next.js?