rsnext/packages/next/shared/lib/router/utils/path-match.ts
JJ Kasper 8c3c2b7ea2
Update redirect regexes to not match _next (#27143)
This updates redirects' regexes to not match `/_next` paths since this is currently unexpected and can easily cause a multi-match redirect to break loading client-side assets. This also fixes custom-routes not matching correctly when `trailingSlash: true/false` is used

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

x-ref: https://github.com/vercel/next.js/discussions/24683
x-ref: [slack thread](https://vercel.slack.com/archives/CGU8HUTUH/p1626159845474000)
2021-07-13 19:38:14 +00:00

52 lines
1.4 KiB
TypeScript

import * as pathToRegexp from 'next/dist/compiled/path-to-regexp'
export { pathToRegexp }
export const matcherOptions: pathToRegexp.TokensToRegexpOptions &
pathToRegexp.ParseOptions = {
sensitive: false,
delimiter: '/',
}
export const customRouteMatcherOptions: pathToRegexp.TokensToRegexpOptions &
pathToRegexp.ParseOptions = {
...matcherOptions,
strict: true,
}
export default (customRoute = false) => {
return (path: string, regexModifier?: (regex: string) => string) => {
const keys: pathToRegexp.Key[] = []
let matcherRegex = pathToRegexp.pathToRegexp(
path,
keys,
customRoute ? customRouteMatcherOptions : matcherOptions
)
if (regexModifier) {
const regexSource = regexModifier(matcherRegex.source)
matcherRegex = new RegExp(regexSource, matcherRegex.flags)
}
const matcher = pathToRegexp.regexpToFunction(matcherRegex, keys)
return (pathname: string | null | undefined, params?: any) => {
const res = pathname == null ? false : matcher(pathname)
if (!res) {
return false
}
if (customRoute) {
for (const key of keys) {
// unnamed params should be removed as they
// are not allowed to be used in the destination
if (typeof key.name === 'number') {
delete (res.params as any)[key.name]
}
}
}
return { ...params, ...res.params }
}
}
}