Next.js Discord

Discord Forum

NextJS middleware custom regexp matcher not working?

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I have this as middleware, which is basically the same code as the official NextJS website:
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:
export const config = {
    matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg|register).*)"]
}

and it works
View full answer

2 Replies

Transvaal lionOP
Nevermind. This code does not work:
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:
export const config = {
    matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\.svg|register).*)"]
}

and it works
Answer