Open anni1236012 opened 7 months ago
@anni1236012 try setting the status code to 303. One of the reasons it might fail is because of the browser cache. By default NextResponse.redirect will do a 307, which can be cahced by the browser.
return NextResponse.redirect(new URL("/login", request.url), {
status: 303,
});
@SuhelMakkad it did not change the behavior.
Interesting 🤔 I am using this exact setup for my app and it works fine. I tried your repo on my local machine, and after the changes it worked. Maybe you need to clear your browser cache.
I have exactly the same problem, it does not give the expected behaviour for a while
I fixed it with rewrite instead of redirect.
return NextResponse.rewrite(new URL("/login", request.url), {
status: 303,
});
@anni1236012 no, the problem is not solved. a short time still does not give the expected result
@yasermazlum Please share your github link to recreate the issue.
try to avoid loading cached page append random query on querystring ex) localhost:3000/hello=${Date.now()}
I also stucked at login page, when I debug see that The user object found and returns true response. But the status code is 303
At NextJS 15 (canary version) it was fixed, the server middleware is correct changing the browser URL after NextResponse.redirect
.
But I guess I found another error when passing a hash parameter /some-url#value-here
, the hash parameter is not sending with the redirected URL.
The problem was not completely fixed.
I am experiencing an issue with the middleware. I need to route the user based on their role
, but the middleware is not able to route properly. Here's the explanation:
I have two roles in my app, manager
and staff
, and a user can have either of these roles in their respective org
.
When the user changes the org
using a dropdown in the UI, the middleware is supposed to detect the new selected org
and route the user to the corresponding URL. I've written the following logic for this:
if (orgRole === 'manager' && request.nextUrl.pathname.includes('/staff')) {
const url = request.nextUrl.clone()
url.pathname = `${orgShortId}/dashboard/manager/app`
return NextResponse.redirect(url, { status: 303 })
}
if (orgRole === 'staff' && request.nextUrl.pathname.includes('/manager')) {
const url = request.nextUrl.clone()
url.pathname = `${orgShortId}/dashboard/staff/app`
return NextResponse.redirect(url, { status: 303 })
}
However, the issue I'm facing is that the app is not getting routed to the URL as expected; it remains unchanged.
might need to use router.refresh
Link to the code that reproduces this issue
https://github.com/anni1236012/nextjsMiddleware
To Reproduce
https://github.com/vercel/next.js/assets/86418669/67bb7e52-b6a3-41c0-b4ad-3eed1eff8404
Current vs. Expected behavior
Delayed navigation, looks like all requests are not reaching middleware and served from cache.
Verify canary release
Provide environment information
Which area(s) are affected? (Select all that apply)
Middleware / Edge (API routes, runtime)
Additional context
No response