Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js middleware matcher negate path

I have a middleware on a Next.js project, and I want to negate my /api/* route.

In other words, I want middleware to run for every route except anything that starts with /api/. I couldn't find an example in the docs.

How do I achieve that (of course, without writing all included routes one by one)?

like image 502
Can Poyrazoğlu Avatar asked Jun 06 '26 00:06

Can Poyrazoğlu


1 Answers

Looks like the middleware docs have been updated to account for something like this.

nextjs middleware docs

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - static (static files)
     * - favicon.ico (favicon file)
     */
    '/((?!api|static|favicon.ico).*)',
  ],
}
like image 119
SQ257 Avatar answered Jun 08 '26 12:06

SQ257