Commit 22e015f7 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Support unnamed parameters in custom-routes correctly (#9920)

* Support unnamed parameters in custom-routes correctly

* Update unnamed params checking to be strict

* Removed un-used regexKeys

* Update test routes
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent 6804039e
...@@ -251,7 +251,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -251,7 +251,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
} }
: {}), : {}),
regex: routeRegex.source, regex: routeRegex.source,
regexKeys: keys.map(k => k.name),
} }
} }
......
...@@ -4,18 +4,42 @@ export { pathToRegexp } ...@@ -4,18 +4,42 @@ export { pathToRegexp }
export default (customRoute = false) => { export default (customRoute = false) => {
return (path: string) => { return (path: string) => {
const matcher = pathToRegexp.match(path, { const keys: pathToRegexp.Key[] = []
const matcherOptions = {
sensitive: false, sensitive: false,
delimiter: '/', delimiter: '/',
...(customRoute ? { strict: true } : undefined), ...(customRoute ? { strict: true } : undefined),
decode: decodeParam, decode: decodeParam,
}) }
const matcherRegex = pathToRegexp.pathToRegexp(path, keys, matcherOptions)
const matcher = pathToRegexp.regexpToFunction(
matcherRegex,
keys,
matcherOptions
)
return (pathname: string | null | undefined, params?: any) => { return (pathname: string | null | undefined, params?: any) => {
const res = pathname == null ? false : matcher(pathname) const res = pathname == null ? false : matcher(pathname)
if (!res) { if (!res) {
return false return false
} }
if (customRoute) {
const newParams: { [k: string]: string } = {}
for (const key of keys) {
// unnamed matches should always be a number while named
// should be a string
if (typeof key.name === 'number') {
newParams[key.name + 1 + ''] = (res.params as any)[key.name + '']
delete (res.params as any)[key.name + '']
}
}
res.params = {
...res.params,
...newParams,
}
}
return { ...params, ...res.params } return { ...params, ...res.params }
} }
} }
......
...@@ -124,6 +124,16 @@ module.exports = { ...@@ -124,6 +124,16 @@ module.exports = {
destination: '/with-params?first=:section&second=:name', destination: '/with-params?first=:section&second=:name',
permanent: false, permanent: false,
}, },
{
source: '/unnamed/(first|second)/(.*)',
destination: '/:1/:2',
permanent: false,
},
{
source: '/named-like-unnamed/:0',
destination: '/:0',
permanent: false,
},
] ]
}, },
......
...@@ -229,6 +229,29 @@ const runTests = (isDev = false) => { ...@@ -229,6 +229,29 @@ const runTests = (isDev = false) => {
expect(res.headers.get('x-second-header')).toBe('second') expect(res.headers.get('x-second-header')).toBe('second')
}) })
it('should support unnamed parameters correctly', async () => {
const res = await fetchViaHTTP(appPort, '/unnamed/first/final', undefined, {
redirect: 'manual',
})
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/first/final')
})
it('should support named like unnamed parameters correctly', async () => {
const res = await fetchViaHTTP(
appPort,
'/named-like-unnamed/first',
undefined,
{
redirect: 'manual',
}
)
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/first')
})
it('should add refresh header for 308 redirect', async () => { it('should add refresh header for 308 redirect', async () => {
const res = await fetchViaHTTP(appPort, '/redirect4', undefined, { const res = await fetchViaHTTP(appPort, '/redirect4', undefined, {
redirect: 'manual', redirect: 'manual',
...@@ -243,7 +266,12 @@ const runTests = (isDev = false) => { ...@@ -243,7 +266,12 @@ const runTests = (isDev = false) => {
join(appDir, '.next/routes-manifest.json') join(appDir, '.next/routes-manifest.json')
) )
for (const route of [...manifest.dynamicRoutes, ...manifest.rewrites]) { for (const route of [
...manifest.dynamicRoutes,
...manifest.rewrites,
...manifest.redirects,
...manifest.headers,
]) {
route.regex = normalizeRegEx(route.regex) route.regex = normalizeRegEx(route.regex)
} }
...@@ -256,82 +284,70 @@ const runTests = (isDev = false) => { ...@@ -256,82 +284,70 @@ const runTests = (isDev = false) => {
destination: '/docs/v2/network/status-codes#:code', destination: '/docs/v2/network/status-codes#:code',
statusCode: 301, statusCode: 301,
regex: normalizeRegEx('^\\/docs\\/router-status(?:\\/([^\\/]+?))$'), regex: normalizeRegEx('^\\/docs\\/router-status(?:\\/([^\\/]+?))$'),
regexKeys: ['code'],
}, },
{ {
source: '/docs/github', source: '/docs/github',
destination: '/docs/v2/advanced/now-for-github', destination: '/docs/v2/advanced/now-for-github',
statusCode: 301, statusCode: 301,
regex: normalizeRegEx('^\\/docs\\/github$'), regex: normalizeRegEx('^\\/docs\\/github$'),
regexKeys: [],
}, },
{ {
source: '/docs/v2/advanced/:all(.*)', source: '/docs/v2/advanced/:all(.*)',
destination: '/docs/v2/more/:all', destination: '/docs/v2/more/:all',
statusCode: 301, statusCode: 301,
regex: normalizeRegEx('^\\/docs\\/v2\\/advanced(?:\\/(.*))$'), regex: normalizeRegEx('^\\/docs\\/v2\\/advanced(?:\\/(.*))$'),
regexKeys: ['all'],
}, },
{ {
source: '/hello/:id/another', source: '/hello/:id/another',
destination: '/blog/:id', destination: '/blog/:id',
statusCode: 307, statusCode: 307,
regex: normalizeRegEx('^\\/hello(?:\\/([^\\/]+?))\\/another$'), regex: normalizeRegEx('^\\/hello(?:\\/([^\\/]+?))\\/another$'),
regexKeys: ['id'],
}, },
{ {
source: '/redirect1', source: '/redirect1',
destination: '/', destination: '/',
statusCode: 307, statusCode: 307,
regex: normalizeRegEx('^\\/redirect1$'), regex: normalizeRegEx('^\\/redirect1$'),
regexKeys: [],
}, },
{ {
source: '/redirect2', source: '/redirect2',
destination: '/', destination: '/',
statusCode: 301, statusCode: 301,
regex: normalizeRegEx('^\\/redirect2$'), regex: normalizeRegEx('^\\/redirect2$'),
regexKeys: [],
}, },
{ {
source: '/redirect3', source: '/redirect3',
destination: '/another', destination: '/another',
statusCode: 302, statusCode: 302,
regex: normalizeRegEx('^\\/redirect3$'), regex: normalizeRegEx('^\\/redirect3$'),
regexKeys: [],
}, },
{ {
source: '/redirect4', source: '/redirect4',
destination: '/', destination: '/',
statusCode: 308, statusCode: 308,
regex: normalizeRegEx('^\\/redirect4$'), regex: normalizeRegEx('^\\/redirect4$'),
regexKeys: [],
}, },
{ {
source: '/redir-chain1', source: '/redir-chain1',
destination: '/redir-chain2', destination: '/redir-chain2',
statusCode: 301, statusCode: 301,
regex: normalizeRegEx('^\\/redir-chain1$'), regex: normalizeRegEx('^\\/redir-chain1$'),
regexKeys: [],
}, },
{ {
source: '/redir-chain2', source: '/redir-chain2',
destination: '/redir-chain3', destination: '/redir-chain3',
statusCode: 302, statusCode: 302,
regex: normalizeRegEx('^\\/redir-chain2$'), regex: normalizeRegEx('^\\/redir-chain2$'),
regexKeys: [],
}, },
{ {
source: '/redir-chain3', source: '/redir-chain3',
destination: '/', destination: '/',
statusCode: 303, statusCode: 303,
regex: normalizeRegEx('^\\/redir-chain3$'), regex: normalizeRegEx('^\\/redir-chain3$'),
regexKeys: [],
}, },
{ {
destination: 'https://google.com', destination: 'https://google.com',
regex: normalizeRegEx('^\\/to-external$'), regex: normalizeRegEx('^\\/to-external$'),
regexKeys: [],
source: '/to-external', source: '/to-external',
statusCode: 307, statusCode: 307,
}, },
...@@ -340,10 +356,23 @@ const runTests = (isDev = false) => { ...@@ -340,10 +356,23 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx( regex: normalizeRegEx(
'^\\/query-redirect(?:\\/([^\\/]+?))(?:\\/([^\\/]+?))$' '^\\/query-redirect(?:\\/([^\\/]+?))(?:\\/([^\\/]+?))$'
), ),
regexKeys: ['section', 'name'],
source: '/query-redirect/:section/:name', source: '/query-redirect/:section/:name',
statusCode: 307, statusCode: 307,
}, },
{
destination: '/:1/:2',
regex: normalizeRegEx(
'^\\/unnamed(?:\\/(first|second))(?:\\/(.*))$'
),
source: '/unnamed/(first|second)/(.*)',
statusCode: 307,
},
{
destination: '/:0',
regex: normalizeRegEx('^\\/named-like-unnamed(?:\\/([^\\/]+?))$'),
source: '/named-like-unnamed/:0',
statusCode: 307,
},
], ],
headers: [ headers: [
{ {
...@@ -358,7 +387,6 @@ const runTests = (isDev = false) => { ...@@ -358,7 +387,6 @@ const runTests = (isDev = false) => {
}, },
], ],
regex: normalizeRegEx('^\\/add-header$'), regex: normalizeRegEx('^\\/add-header$'),
regexKeys: [],
source: '/add-header', source: '/add-header',
}, },
{ {
...@@ -373,7 +401,6 @@ const runTests = (isDev = false) => { ...@@ -373,7 +401,6 @@ const runTests = (isDev = false) => {
}, },
], ],
regex: normalizeRegEx('^\\/my-headers(?:\\/(.*))$'), regex: normalizeRegEx('^\\/my-headers(?:\\/(.*))$'),
regexKeys: [0],
source: '/my-headers/(.*)', source: '/my-headers/(.*)',
}, },
], ],
...@@ -381,56 +408,47 @@ const runTests = (isDev = false) => { ...@@ -381,56 +408,47 @@ const runTests = (isDev = false) => {
{ {
destination: '/another/one', destination: '/another/one',
regex: normalizeRegEx('^\\/to-another$'), regex: normalizeRegEx('^\\/to-another$'),
regexKeys: [],
source: '/to-another', source: '/to-another',
}, },
{ {
source: '/hello-world', source: '/hello-world',
destination: '/static/hello.txt', destination: '/static/hello.txt',
regex: normalizeRegEx('^\\/hello-world$'), regex: normalizeRegEx('^\\/hello-world$'),
regexKeys: [],
}, },
{ {
source: '/', source: '/',
destination: '/another', destination: '/another',
regex: normalizeRegEx('^\\/$'), regex: normalizeRegEx('^\\/$'),
regexKeys: [],
}, },
{ {
source: '/another', source: '/another',
destination: '/multi-rewrites', destination: '/multi-rewrites',
regex: normalizeRegEx('^\\/another$'), regex: normalizeRegEx('^\\/another$'),
regexKeys: [],
}, },
{ {
source: '/first', source: '/first',
destination: '/hello', destination: '/hello',
regex: normalizeRegEx('^\\/first$'), regex: normalizeRegEx('^\\/first$'),
regexKeys: [],
}, },
{ {
source: '/second', source: '/second',
destination: '/hello-again', destination: '/hello-again',
regex: normalizeRegEx('^\\/second$'), regex: normalizeRegEx('^\\/second$'),
regexKeys: [],
}, },
{ {
destination: '/hello', destination: '/hello',
regex: normalizeRegEx('^\\/to-hello$'), regex: normalizeRegEx('^\\/to-hello$'),
regexKeys: [],
source: '/to-hello', source: '/to-hello',
}, },
{ {
destination: '/blog/post-2', destination: '/blog/post-2',
regex: normalizeRegEx('^\\/blog\\/post-1$'), regex: normalizeRegEx('^\\/blog\\/post-1$'),
regexKeys: [],
source: '/blog/post-1', source: '/blog/post-1',
}, },
{ {
source: '/test/:path', source: '/test/:path',
destination: '/:path', destination: '/:path',
regex: normalizeRegEx('^\\/test(?:\\/([^\\/]+?))$'), regex: normalizeRegEx('^\\/test(?:\\/([^\\/]+?))$'),
regexKeys: ['path'],
}, },
{ {
source: '/test-overwrite/:something/:another', source: '/test-overwrite/:something/:another',
...@@ -438,20 +456,17 @@ const runTests = (isDev = false) => { ...@@ -438,20 +456,17 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx( regex: normalizeRegEx(
'^\\/test-overwrite(?:\\/([^\\/]+?))(?:\\/([^\\/]+?))$' '^\\/test-overwrite(?:\\/([^\\/]+?))(?:\\/([^\\/]+?))$'
), ),
regexKeys: ['something', 'another'],
}, },
{ {
source: '/params/:something', source: '/params/:something',
destination: '/with-params', destination: '/with-params',
regex: normalizeRegEx('^\\/params(?:\\/([^\\/]+?))$'), regex: normalizeRegEx('^\\/params(?:\\/([^\\/]+?))$'),
regexKeys: ['something'],
}, },
{ {
destination: '/with-params?first=:section&second=:name', destination: '/with-params?first=:section&second=:name',
regex: normalizeRegEx( regex: normalizeRegEx(
'^\\/query-rewrite(?:\\/([^\\/]+?))(?:\\/([^\\/]+?))$' '^\\/query-rewrite(?:\\/([^\\/]+?))(?:\\/([^\\/]+?))$'
), ),
regexKeys: ['section', 'name'],
source: '/query-rewrite/:section/:name', source: '/query-rewrite/:section/:name',
}, },
{ {
...@@ -459,7 +474,6 @@ const runTests = (isDev = false) => { ...@@ -459,7 +474,6 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx( regex: normalizeRegEx(
'^\\/hidden\\/_next(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?$' '^\\/hidden\\/_next(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?$'
), ),
regexKeys: ['path'],
source: '/hidden/_next/:path*', source: '/hidden/_next/:path*',
}, },
], ],
......
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