rsnext/errors/invalid-route-source.md
Damien Simonin Feugas d8395e4eeb
docs: more detailed error message when failing to parse a middleware matcher (#41390)
## 📖 What's in there?

This is a follow-up for https://github.com/vercel/next.js/pull/40180
Someone made the good point that users may get directed to this error page while setting up middleware matchers. And that page has no information (yet) about it.

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)



Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
2022-10-13 18:03:10 +00:00

1,014 B

Invalid Custom Route source

Why This Error Occurred

When defining custom routes, or a middleware matcher, a pattern could not be parsed.

This could have been due to trying to use normal RegExp syntax like negative lookaheads (?!exclude) without following path-to-regexp's syntax for it.

Possible Ways to Fix It

Wrap the RegExp part of your source as an un-named parameter.


Custom routes:

Before

{
  source: '/feedback/(?!general)',
  destination: '/feedback/general'
}

After

{
  source: '/feedback/((?!general).*)',
  destination: '/feedback/general'
}

Middleware:

Before

const config = {
  matcher: '/feedback/(?!general)',
}

After

const config = {
  matcher: '/feedback/((?!general).*)',
}