Next.js Discord

Discord Forum

make middleware.ts matcher work on all routes

Answered
Polar bear posted this in #help-forum
Open in Discord
Avatar
Polar bearOP
export const config = {
  matcher: ["/", "/dashboard"],
};


Hey guys, the above is my current middleware.ts matcher code.
Is it possible to change this in order for it to work on all routes by default?

Removing that code snippet from the middleware.ts or doing something like "/(.*)" results in the browser logs to say what's in the picture (I am using app router with src/ directory):
Image
Answered by B33fb0n3
oh, you can change this example as well:
export const config = {
  matcher: [
    /*
     * 

Match all request paths except for the ones starting with:

     * - api (API routes)
     * - _next/static (static files)
     */
    '/((?!api|_next/static).*)',
  ],
}
View full answer

7 Replies

Avatar
B33fb0n3
you can do something like this:
export const config = {
  matcher: [
    /*
     * 

Match all request paths except for the ones starting with:

     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico (favicon file)
     */
    '/((?!api|_next/static|_next/image|favicon.ico).*)',
  ],
}
Avatar
Polar bearOP
but that results in my favicon and images to not being loaded anymore
I tried that
Avatar
B33fb0n3
oh, you can change this example as well:
export const config = {
  matcher: [
    /*
     * 

Match all request paths except for the ones starting with:

     * - api (API routes)
     * - _next/static (static files)
     */
    '/((?!api|_next/static).*)',
  ],
}
Answer
Avatar
B33fb0n3
@Polar bear solved?
Avatar
Polar bearOP
solved tyvm
Avatar
joulev
(by the way if you want to run middleware in truly every route, simply remove the matcher)