next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Docs: middleware matcher config sample should be explicit about root route

Open dmudro opened this issue 2 years ago • 0 comments

What is the improvement or update you wish to see?

The config sample on middleware docs page states

To match all request paths except for the ones starting with:

export const config = {
  matcher: ['/((?!api|_next/static|favicon.ico).*)'],
};

In my experience, root route /, i. e. homepage is not caught by this regex which I think is expected given the negative look ahead with .*.

My workaround is adding a separate matcher:

export const config = {
  matcher: [
    '/((?!api|_next/static|favicon.ico).*)',
    '/', // explicit matcher for root route
  ],
};

The docs should be more clear about root route excluded from middleware execution.

Is there any context that might help us understand?

The docs should be more clear about root route excluded from middleware execution.

It's easy to miss it when using the current config sample.

Does the docs page already exist? Please link to it.

https://vercel.com/docs/functions/edge-middleware/middleware-api#match-based-on-a-negative-lookahead

dmudro avatar Feb 15 '24 07:02 dmudro