Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unnamed parameters in custom-routes correctly #9920

Merged
merged 14 commits into from Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/next/next-server/server/lib/path-match.ts
Expand Up @@ -16,6 +16,20 @@ export default (customRoute = false) => {
if (!res) {
return false
}

if (customRoute) {
const newParams: { [k: string]: string } = {}
Object.keys(res.params).forEach((key, idx) => {
if (key === idx + '') {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
newParams[idx + 1 + ''] = (res.params as any)[key]
}
})
res.params = {
...res.params,
...newParams,
}
}

return { ...params, ...res.params }
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/custom-routes/next.config.js
Expand Up @@ -120,6 +120,10 @@ module.exports = {
source: '/query-redirect/:section/:name',
destination: '/with-params?first=:section&second=:name',
},
{
source: '/unnamed/(first|second)/(.*)',
destination: '/:1/:2',
},
]
},

Expand Down
18 changes: 18 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Expand Up @@ -229,6 +229,15 @@ const runTests = (isDev = false) => {
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')
})

if (!isDev) {
it('should output routes-manifest successfully', async () => {
const manifest = await fs.readJSON(
Expand Down Expand Up @@ -336,6 +345,15 @@ const runTests = (isDev = false) => {
source: '/query-redirect/:section/:name',
statusCode: 307,
},
{
destination: '/:1/:2',
regex: normalizeRegEx(
'^\\/unnamed(?:\\/(first|second))(?:\\/(.*))$'
),
regexKeys: [0, 1],
source: '/unnamed/(first|second)/(.*)',
statusCode: 307,
},
],
headers: [
{
Expand Down