Skip to content

Commit

Permalink
docs: more detailed error message when failing to parse a middleware …
Browse files Browse the repository at this point in the history
…matcher (vercel#41390)

## 📖 What's in there?

This is a follow-up for vercel#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>
  • Loading branch information
2 people authored and Kikobeats committed Oct 24, 2022
1 parent 7ed106c commit 7b90a58
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion errors/invalid-route-source.md
Expand Up @@ -2,12 +2,18 @@

#### Why This Error Occurred

When defining custom routes a route was added that causes an error during parsing. This can be due to trying to use normal `RegExp` syntax like negative lookaheads (`?!exclude`) without following `path-to-regexp`'s syntax for it.
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`](https://github.com/pillarjs/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**

```js
Expand All @@ -26,6 +32,26 @@ Wrap the `RegExp` part of your `source` as an un-named parameter.
}
```

---

Middleware:

**Before**

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

**After**

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

### Useful Links

- [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
Expand Down

0 comments on commit 7b90a58

Please sign in to comment.