Redirect doesn't change search bar url
Unanswered
Wuchang bream posted this in #help-forum
Wuchang breamOP
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const user = await getUser(req, res);
const isRestricted = await isRouteRestricted(req, user);
if (isRestricted) {
return NextResponse.redirect(req.nextUrl.origin);
}
return res;
}when i redirect user using
NextResponse.redirect(req.nextUrl.origin) it doesn't change the window search bar URL.14 Replies
from the example here, I think you'd need to change it to
https://nextjs.org/docs/app/building-your-application/routing/middleware#example
NextResponse.redirect(new URL('/', req.url))import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url))
}
// See "Matching Paths" below to learn more
export const config = {
matcher: '/about/:path*',
}https://nextjs.org/docs/app/building-your-application/routing/middleware#example
Wuchang breamOP
i did change it
return NextResponse.redirect(new URL("/", req.url));but searchbar url not updating
@Wuchang bream but searchbar url not updating
What do you mean searchbar url?
Wuchang breamOP
does calling
router.refresh() again calls middleware.ts ?Did you set matching paths in your middleware?
// See "Matching Paths" below to learn more
export const config = {
matcher: '/about/:path*',
}Wuchang breamOP
search url
@Marchy Did you set matching paths in your middleware?
Wuchang breamOP
like i want the middlware for every route
@Wuchang bream like i want the middlware for every route
You still need to define it. Keep in mind that if you include your /login route it will endlessly redirect
Wuchang breamOP
can you show me docs to implement protected routes in nextjs 13 ?
i am trying to implement protected routes using
middleware.ts is it the right place to do ?Wuchang breamOP
ðŸ‘