NextJS middleware custom regexp matcher not working?
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
I have this as middleware, which is basically the same code as the official NextJS website:
This would be matching every route that it would access, I only wanted the middleware to access anything BUT a route called
I'm leaving this here, just in case someone needs it or to see if my approach was incorrect.
export function middleware(request:NextRequest){
//do something
return NextResponse.next()
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg).*)", "/((?!register).*)"]
}This would be matching every route that it would access, I only wanted the middleware to access anything BUT a route called
register, so I removed the .* from both regexp and it worked!export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg))", "/((?!register))"]
}I'm leaving this here, just in case someone needs it or to see if my approach was incorrect.
Answered by Transvaal lion
I changed the matcher to:
and it works
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg|register).*)"]
}and it works
2 Replies
Transvaal lionOP
Nevermind. This code does not work:
It's not taking any route. Not sure why.
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg))", "/((?!register))"]
}It's not taking any route. Not sure why.
Transvaal lionOP
I changed the matcher to:
and it works
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg|register).*)"]
}and it works
Answer