NextResponse redirect with Header Prop #51232
-
|
How would one issue a redirect within middleware, while also setting custom headers? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
|
This is one of a way to do it. import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === '/') {
const response = NextResponse.redirect(new URL('/test', request.nextUrl))
response.headers.set('some-header', 'hello')
return response
}
}Reference |
Beta Was this translation helpful? Give feedback.
-
|
Is anyone else experiencing this issue? It's causing significant problems. I've been investigating and have formed some theories about what might be happening: Here's an example to help explain:
This is just my understanding based on what I've observed. If anyone has experienced something similar or has more insights, please share. |
Beta Was this translation helpful? Give feedback.
-
|
or just simply |
Beta Was this translation helpful? Give feedback.
-
|
@ZowWeb Did you get this to work on your side or are you guessing? Chaining a header doesn't work on 15.1.4. |
Beta Was this translation helpful? Give feedback.
-
|
EDIT: spoke too soon, not working as expected on 15.5 this worked for me: return NextResponse.redirect(new URL(destination, request.url), { |
Beta Was this translation helpful? Give feedback.
This is one of a way to do it.
Reference