Unverified Commit 163f6071 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add parsing of route source during checking (#9787)

* Add parsing of route during checking

* Update err.sh
parent 8bf8c865
# Invalid Custom Route `source`
#### 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.
#### Possible Ways to Fix It
Wrap the `RegExp` part of your `source` as an un-named parameter.
**Before**
```js
{
source: '/feedback/(?!general)',
destination: '/feedback/general'
}
```
**After**
```js
{
source: '/feedback/((?!general).*)',
destination: '/feedback/general'
}
```
### Useful Links
- [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
- [un-named paramters](https://github.com/pillarjs/path-to-regexp#unnamed-parameters)
import { match as regexpMatch } from 'path-to-regexp'
export type Rewrite = { export type Rewrite = {
source: string source: string
destination: string destination: string
...@@ -56,6 +58,17 @@ export default function checkCustomRoutes( ...@@ -56,6 +58,17 @@ export default function checkCustomRoutes(
} }
} }
try {
// Make sure we can parse the source properly
regexpMatch(route.source)
} catch (err) {
// If there is an error show our err.sh but still show original error
console.error(
`\nError parsing ${route.source} https://err.sh/zeit/next.js/invalid-route-source`,
err
)
}
const hasInvalidKeys = invalidKeys.length > 0 const hasInvalidKeys = invalidKeys.length > 0
const hasInvalidParts = invalidParts.length > 0 const hasInvalidParts = invalidParts.length > 0
......
...@@ -97,6 +97,10 @@ const invalidRewrites = [ ...@@ -97,6 +97,10 @@ const invalidRewrites = [
source: '/hello', source: '/hello',
destination: 'another', destination: 'another',
}, },
{
source: '/feedback/(?!general)',
destination: '/feedback/general',
},
] ]
const invalidRewriteAssertions = (stderr = '') => { const invalidRewriteAssertions = (stderr = '') => {
...@@ -120,6 +124,10 @@ const invalidRewriteAssertions = (stderr = '') => { ...@@ -120,6 +124,10 @@ const invalidRewriteAssertions = (stderr = '') => {
`\`destination\` does not start with / for route {"source":"/hello","destination":"another"}` `\`destination\` does not start with / for route {"source":"/hello","destination":"another"}`
) )
expect(stderr).toContain(
`Error parsing /feedback/(?!general) https://err.sh/zeit/next.js/invalid-route-source TypeError: Pattern cannot start with "?"`
)
expect(stderr).not.toContain( expect(stderr).not.toContain(
'Valid redirect statusCode values are 301, 302, 303, 307, 308' 'Valid redirect statusCode values are 301, 302, 303, 307, 308'
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment