Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dmudro opened this issue Feb 15, 2024 · 1 comment
Open

Comments

@dmudro
Copy link

dmudro commented Feb 15, 2024

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 dmudro added the Documentation Related to Next.js' official documentation. label Feb 15, 2024
@dmudro dmudro changed the title Docs: Docs: middleware matcher config sample should be explicit about root route Feb 15, 2024
@dmudro
Copy link
Author

dmudro commented Feb 22, 2024

this may or may not be a documentation issue.

the extra matcher does work when deployed in Vercel - middleware catches the root route:

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

however, the extra matcher breaks localhost:

image

removing the / matcher fixes localhost but then root route is not caught by middleware when deployed.

@samcx samcx removed the Documentation Related to Next.js' official documentation. label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants