Next.js Discord

Discord Forum

make middleware.ts matcher work on all routes

Answered
Polar bear posted this in #help-forum
Open in Discord
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):
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

@Polar bear ts 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):
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).*)',
  ],
}
Polar bearOP
but that results in my favicon and images to not being loaded anymore
I tried that
@Polar bear but that results in my favicon and images to not being loaded anymore
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
@Polar bear solved?
Polar bearOP
solved tyvm
(by the way if you want to run middleware in truly every route, simply remove the matcher)