Unverified Commit 65c22167 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure has segments are allowed in destination (#23588)

This ensures we gather segments from the experimental has field when validating segments used in the destination to prevent the invalid segments in the destination error from showing incorrectly. This usage has been added to the custom-routes test suite to ensure the segments are passed correctly from the has field. 

Fixes: https://github.com/vercel/next.js/issues/23415

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
parent 0d5baf2c
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import { execOnce } from '../next-server/lib/utils' import { execOnce } from '../next-server/lib/utils'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { getSafeParamName } from '../next-server/lib/router/utils/prepare-destination'
export type RouteHas = export type RouteHas =
| { | {
...@@ -325,6 +326,34 @@ function checkCustomRoutes( ...@@ -325,6 +326,34 @@ function checkCustomRoutes(
} }
sourceTokens = tokens sourceTokens = tokens
} }
const hasSegments = new Set<string>()
if (route.has) {
for (const hasItem of route.has) {
if (!hasItem.value && hasItem.key) {
hasSegments.add(hasItem.key)
}
if (hasItem.value) {
const matcher = new RegExp(`^${hasItem.value}$`)
const matches = matcher.exec('')
if (matches) {
if (matches.groups) {
Object.keys(matches.groups).forEach((groupKey) => {
const safeKey = getSafeParamName(groupKey)
if (safeKey && matches.groups![groupKey]) {
hasSegments.add(safeKey)
}
})
} else {
hasSegments.add(hasItem.key || 'host')
}
}
}
}
}
// make sure no unnamed patterns are attempted to be used in the // make sure no unnamed patterns are attempted to be used in the
// destination as this can cause confusion and is not allowed // destination as this can cause confusion and is not allowed
...@@ -369,7 +398,8 @@ function checkCustomRoutes( ...@@ -369,7 +398,8 @@ function checkCustomRoutes(
for (const token of destTokens!) { for (const token of destTokens!) {
if ( if (
typeof token === 'object' && typeof token === 'object' &&
!sourceSegments.has(token.name) !sourceSegments.has(token.name) &&
!hasSegments.has(token.name as string)
) { ) {
invalidDestSegments.add(token.name) invalidDestSegments.add(token.name)
} }
...@@ -377,7 +407,7 @@ function checkCustomRoutes( ...@@ -377,7 +407,7 @@ function checkCustomRoutes(
if (invalidDestSegments.size) { if (invalidDestSegments.size) {
invalidParts.push( invalidParts.push(
`\`destination\` has segments not in \`source\` (${[ `\`destination\` has segments not in \`source\` or \`has\` (${[
...invalidDestSegments, ...invalidDestSegments,
].join(', ')})` ].join(', ')})`
) )
......
...@@ -9,7 +9,7 @@ type Params = { [param: string]: any } ...@@ -9,7 +9,7 @@ type Params = { [param: string]: any }
// ensure only a-zA-Z are used for param names for proper interpolating // ensure only a-zA-Z are used for param names for proper interpolating
// with path-to-regexp // with path-to-regexp
const getSafeParamName = (paramName: string) => { export const getSafeParamName = (paramName: string) => {
let newParamName = '' let newParamName = ''
for (let i = 0; i < paramName.length; i++) { for (let i = 0; i < paramName.length; i++) {
......
...@@ -149,6 +149,16 @@ module.exports = { ...@@ -149,6 +149,16 @@ module.exports = {
], ],
destination: '/with-params?host=1', destination: '/with-params?host=1',
}, },
{
source: '/has-rewrite-5',
has: [
{
type: 'query',
key: 'hasParam',
},
],
destination: '/:hasParam',
},
], ],
beforeFiles: [ beforeFiles: [
{ {
......
...@@ -692,6 +692,22 @@ const runTests = (isDev = false) => { ...@@ -692,6 +692,22 @@ const runTests = (isDev = false) => {
expect(res2.status).toBe(404) expect(res2.status).toBe(404)
}) })
it('should pass has segment for rewrite correctly', async () => {
const res1 = await fetchViaHTTP(appPort, '/has-rewrite-5')
expect(res1.status).toBe(404)
const res = await fetchViaHTTP(appPort, '/has-rewrite-5', {
hasParam: 'with-params',
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
hasParam: 'with-params',
})
})
it('should match has rewrite correctly before files', async () => { it('should match has rewrite correctly before files', async () => {
const res1 = await fetchViaHTTP(appPort, '/hello') const res1 = await fetchViaHTTP(appPort, '/hello')
expect(res1.status).toBe(200) expect(res1.status).toBe(200)
...@@ -1509,6 +1525,17 @@ const runTests = (isDev = false) => { ...@@ -1509,6 +1525,17 @@ const runTests = (isDev = false) => {
regex: '^\\/has-rewrite-4$', regex: '^\\/has-rewrite-4$',
source: '/has-rewrite-4', source: '/has-rewrite-4',
}, },
{
destination: '/:hasParam',
has: [
{
key: 'hasParam',
type: 'query',
},
],
regex: normalizeRegEx('^\\/has-rewrite-5$'),
source: '/has-rewrite-5',
},
], ],
fallback: [], fallback: [],
}, },
......
...@@ -542,7 +542,7 @@ const runTests = () => { ...@@ -542,7 +542,7 @@ const runTests = () => {
const stderr = await getStderr() const stderr = await getStderr()
expect(stderr).toContain( expect(stderr).toContain(
`\`destination\` has segments not in \`source\` (id) for route {"source":"/feedback/:type","destination":"/feedback/:id"}` `\`destination\` has segments not in \`source\` or \`has\` (id) for route {"source":"/feedback/:type","destination":"/feedback/:id"}`
) )
}) })
} }
......
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