Unverified Commit 28ac6289 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Reapply "fix: handle optional catchalls correctly" (#75442) (#75450)

This is a reapplication of the [original
fix](https://github.com/vercel/next.js/pull/75377) for optional catchall
routes but narrowed to only when the parameters weren't already valid.
parent 82026906
...@@ -1182,7 +1182,8 @@ export default abstract class Server< ...@@ -1182,7 +1182,8 @@ export default abstract class Server<
let params: ParsedUrlQuery | false = {} let params: ParsedUrlQuery | false = {}
let paramsResult = utils.normalizeDynamicRouteParams( let paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query parsedUrl.query,
false
) )
// for prerendered ISR paths we attempt parsing the route // for prerendered ISR paths we attempt parsing the route
...@@ -1196,7 +1197,7 @@ export default abstract class Server< ...@@ -1196,7 +1197,7 @@ export default abstract class Server<
let matcherParams = utils.dynamicRouteMatcher?.(normalizedUrlPath) let matcherParams = utils.dynamicRouteMatcher?.(normalizedUrlPath)
if (matcherParams) { if (matcherParams) {
utils.normalizeDynamicRouteParams(matcherParams) utils.normalizeDynamicRouteParams(matcherParams, false)
Object.assign(paramsResult.params, matcherParams) Object.assign(paramsResult.params, matcherParams)
paramsResult.hasValidParams = true paramsResult.hasValidParams = true
} }
...@@ -1218,8 +1219,10 @@ export default abstract class Server< ...@@ -1218,8 +1219,10 @@ export default abstract class Server<
let matcherParams = utils.dynamicRouteMatcher?.(matchedPath) let matcherParams = utils.dynamicRouteMatcher?.(matchedPath)
if (matcherParams) { if (matcherParams) {
const curParamsResult = const curParamsResult = utils.normalizeDynamicRouteParams(
utils.normalizeDynamicRouteParams(matcherParams) matcherParams,
false
)
if (curParamsResult.hasValidParams) { if (curParamsResult.hasValidParams) {
Object.assign(params, matcherParams) Object.assign(params, matcherParams)
...@@ -1254,6 +1257,19 @@ export default abstract class Server< ...@@ -1254,6 +1257,19 @@ export default abstract class Server<
} }
} }
// Try to parse the params from the query if we couldn't parse them
// from the route matches but ignore missing optional params.
if (!paramsResult.hasValidParams) {
paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query,
true
)
if (paramsResult.hasValidParams) {
params = paramsResult.params
}
}
// handle the actual dynamic route name being requested // handle the actual dynamic route name being requested
if ( if (
utils.defaultRouteMatches && utils.defaultRouteMatches &&
...@@ -1276,7 +1292,7 @@ export default abstract class Server< ...@@ -1276,7 +1292,7 @@ export default abstract class Server<
} }
if (pageIsDynamic || didRewrite) { if (pageIsDynamic || didRewrite) {
utils.normalizeVercelUrl(req, true, [ utils.normalizeVercelUrl(req, [
...rewriteParamKeys, ...rewriteParamKeys,
...Object.keys(utils.defaultRouteRegex?.groups || {}), ...Object.keys(utils.defaultRouteRegex?.groups || {}),
]) ])
......
...@@ -24,21 +24,19 @@ import { normalizeNextQueryParam } from './web/utils' ...@@ -24,21 +24,19 @@ import { normalizeNextQueryParam } from './web/utils'
export function normalizeVercelUrl( export function normalizeVercelUrl(
req: BaseNextRequest, req: BaseNextRequest,
trustQuery: boolean, paramKeys: string[],
paramKeys?: string[], defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
pageIsDynamic?: boolean,
defaultRouteRegex?: ReturnType<typeof getNamedRouteRegex> | undefined
) { ) {
if (!defaultRouteRegex) return
// make sure to normalize req.url on Vercel to strip dynamic params // make sure to normalize req.url on Vercel to strip dynamic params
// from the query which are added during routing // from the query which are added during routing
if (pageIsDynamic && trustQuery && defaultRouteRegex) {
const _parsedUrl = parseUrl(req.url!, true) const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search delete (_parsedUrl as any).search
for (const key of Object.keys(_parsedUrl.query)) { for (const key of Object.keys(_parsedUrl.query)) {
const isNextQueryPrefix = const isNextQueryPrefix =
key !== NEXT_QUERY_PARAM_PREFIX && key !== NEXT_QUERY_PARAM_PREFIX && key.startsWith(NEXT_QUERY_PARAM_PREFIX)
key.startsWith(NEXT_QUERY_PARAM_PREFIX)
const isNextInterceptionMarkerPrefix = const isNextInterceptionMarkerPrefix =
key !== NEXT_INTERCEPTION_MARKER_PREFIX && key !== NEXT_INTERCEPTION_MARKER_PREFIX &&
...@@ -52,8 +50,8 @@ export function normalizeVercelUrl( ...@@ -52,8 +50,8 @@ export function normalizeVercelUrl(
delete _parsedUrl.query[key] delete _parsedUrl.query[key]
} }
} }
req.url = formatUrl(_parsedUrl) req.url = formatUrl(_parsedUrl)
}
} }
export function interpolateDynamicPath( export function interpolateDynamicPath(
...@@ -89,27 +87,21 @@ export function interpolateDynamicPath( ...@@ -89,27 +87,21 @@ export function interpolateDynamicPath(
} }
export function normalizeDynamicRouteParams( export function normalizeDynamicRouteParams(
params: ParsedUrlQuery, query: ParsedUrlQuery,
ignoreOptional?: boolean, defaultRouteRegex: ReturnType<typeof getNamedRouteRegex>,
defaultRouteRegex?: ReturnType<typeof getNamedRouteRegex> | undefined, defaultRouteMatches: ParsedUrlQuery,
defaultRouteMatches?: ParsedUrlQuery | undefined ignoreMissingOptional: boolean
) { ) {
let hasValidParams = true let hasValidParams = true
if (!defaultRouteRegex) return { params, hasValidParams: false } let params: ParsedUrlQuery = {}
params = Object.keys(defaultRouteRegex.groups).reduce((prev, key) => { for (const key of Object.keys(defaultRouteRegex.groups)) {
let value: string | string[] | undefined = params[key] let value: string | string[] | undefined = query[key]
if (typeof value === 'string') { if (typeof value === 'string') {
value = normalizeRscURL(value) value = normalizeRscURL(value)
} } else if (Array.isArray(value)) {
if (Array.isArray(value)) { value = value.map(normalizeRscURL)
value = value.map((val) => {
if (typeof val === 'string') {
val = normalizeRscURL(val)
}
return val
})
} }
// if the value matches the default value we can't rely // if the value matches the default value we can't rely
...@@ -128,9 +120,9 @@ export function normalizeDynamicRouteParams( ...@@ -128,9 +120,9 @@ export function normalizeDynamicRouteParams(
if ( if (
isDefaultValue || isDefaultValue ||
(typeof value === 'undefined' && !(isOptional && ignoreOptional)) (typeof value === 'undefined' && !(isOptional && ignoreMissingOptional))
) { ) {
hasValidParams = false return { params: {}, hasValidParams: false }
} }
// non-provided optional values should be undefined so normalize // non-provided optional values should be undefined so normalize
...@@ -145,7 +137,7 @@ export function normalizeDynamicRouteParams( ...@@ -145,7 +137,7 @@ export function normalizeDynamicRouteParams(
(value[0] === 'index' || value[0] === `[[...${key}]]`))) (value[0] === 'index' || value[0] === `[[...${key}]]`)))
) { ) {
value = undefined value = undefined
delete params[key] delete query[key]
} }
// query values from the proxy aren't already split into arrays // query values from the proxy aren't already split into arrays
...@@ -159,10 +151,9 @@ export function normalizeDynamicRouteParams( ...@@ -159,10 +151,9 @@ export function normalizeDynamicRouteParams(
} }
if (value) { if (value) {
prev[key] = value params[key] = value
}
} }
return prev
}, {} as ParsedUrlQuery)
return { return {
params, params,
...@@ -375,28 +366,30 @@ export function getUtils({ ...@@ -375,28 +366,30 @@ export function getUtils({
dynamicRouteMatcher, dynamicRouteMatcher,
defaultRouteMatches, defaultRouteMatches,
getParamsFromRouteMatches, getParamsFromRouteMatches,
/**
* Normalize dynamic route params.
*
* @param query - The query params to normalize.
* @param ignoreMissingOptional - Whether to ignore missing optional params.
* @returns The normalized params and whether they are valid.
*/
normalizeDynamicRouteParams: ( normalizeDynamicRouteParams: (
params: ParsedUrlQuery, query: ParsedUrlQuery,
ignoreOptional?: boolean ignoreMissingOptional: boolean
) => ) => {
normalizeDynamicRouteParams( if (!defaultRouteRegex || !defaultRouteMatches) {
params, return { params: {}, hasValidParams: false }
ignoreOptional, }
return normalizeDynamicRouteParams(
query,
defaultRouteRegex, defaultRouteRegex,
defaultRouteMatches defaultRouteMatches,
), ignoreMissingOptional
normalizeVercelUrl: ( )
req: BaseNextRequest, },
trustQuery: boolean, normalizeVercelUrl: (req: BaseNextRequest, paramKeys: string[]) =>
paramKeys?: string[] normalizeVercelUrl(req, paramKeys, defaultRouteRegex),
) =>
normalizeVercelUrl(
req,
trustQuery,
paramKeys,
pageIsDynamic,
defaultRouteRegex
),
interpolateDynamicPath: ( interpolateDynamicPath: (
pathname: string, pathname: string,
params: Record<string, undefined | string | string[]> params: Record<string, undefined | string | string[]>
......
...@@ -173,9 +173,9 @@ export default class NextWebServer extends BaseServer< ...@@ -173,9 +173,9 @@ export default class NextWebServer extends BaseServer<
) as NextParsedUrlQuery ) as NextParsedUrlQuery
const paramsResult = normalizeDynamicRouteParams( const paramsResult = normalizeDynamicRouteParams(
query, query,
false,
routeRegex, routeRegex,
defaultRouteMatches defaultRouteMatches,
false
) )
const normalizedParams = paramsResult.hasValidParams const normalizedParams = paramsResult.hasValidParams
? paramsResult.params ? paramsResult.params
...@@ -186,13 +186,7 @@ export default class NextWebServer extends BaseServer< ...@@ -186,13 +186,7 @@ export default class NextWebServer extends BaseServer<
normalizedParams, normalizedParams,
routeRegex routeRegex
) )
normalizeVercelUrl( normalizeVercelUrl(req, Object.keys(routeRegex.routeKeys), routeRegex)
req,
true,
Object.keys(routeRegex.routeKeys),
true,
routeRegex
)
} }
} }
......
...@@ -82,7 +82,8 @@ export class EdgeRouteModuleWrapper { ...@@ -82,7 +82,8 @@ export class EdgeRouteModuleWrapper {
}) })
const { params } = utils.normalizeDynamicRouteParams( const { params } = utils.normalizeDynamicRouteParams(
searchParamsToUrlQuery(request.nextUrl.searchParams) searchParamsToUrlQuery(request.nextUrl.searchParams),
false
) )
const waitUntil = evt.waitUntil.bind(evt) const waitUntil = evt.waitUntil.bind(evt)
......
import { useRouter } from 'next/router'
export const getStaticProps = ({ params }) => {
return {
props: {
hello: 'world',
params: params || null,
random: Math.random(),
},
}
}
export const getStaticPaths = () => {
return {
paths: ['/partial-catch-all/hello.com'],
fallback: true,
}
}
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="catch-all">partial optional catch-all page</p>
<p id="router">{JSON.stringify(router)}</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
...@@ -328,7 +328,7 @@ describe('Required Server Files', () => { ...@@ -328,7 +328,7 @@ describe('Required Server Files', () => {
{ {
headers: { headers: {
'x-matched-path': '/catch-all/[[...rest]]', 'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': 'nxtPrest=hello&catchAll=hello', 'x-now-route-matches': 'nxtPrest=hello',
}, },
} }
) )
...@@ -342,7 +342,7 @@ describe('Required Server Files', () => { ...@@ -342,7 +342,7 @@ describe('Required Server Files', () => {
const html3 = await renderViaHTTP( const html3 = await renderViaHTTP(
appPort, appPort,
'/catch-all/[[..rest]]', '/catch-all/[[...rest]]',
undefined, undefined,
{ {
headers: { headers: {
...@@ -359,6 +359,98 @@ describe('Required Server Files', () => { ...@@ -359,6 +359,98 @@ describe('Required Server Files', () => {
expect(data3.params).toEqual({ rest: ['hello', 'world'] }) expect(data3.params).toEqual({ rest: ['hello', 'world'] })
expect(isNaN(data3.random)).toBe(false) expect(isNaN(data3.random)).toBe(false)
expect(data3.random).not.toBe(data.random) expect(data3.random).not.toBe(data.random)
const html4 = await renderViaHTTP(
appPort,
'/catch-all/[[...rest]]',
{ nxtPrest: 'frank' },
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
)
const $4 = cheerio.load(html4)
const data4 = JSON.parse($4('#props').text())
expect($4('#catch-all').text()).toBe('optional catch-all page')
expect(data4.params).toEqual({ rest: ['frank'] })
expect(isNaN(data4.random)).toBe(false)
expect(data4.random).not.toBe(data.random)
const html5 = await renderViaHTTP(
appPort,
'/catch-all/[[...rest]]',
{},
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
)
const $5 = cheerio.load(html5)
const data5 = JSON.parse($5('#props').text())
expect($5('#catch-all').text()).toBe('optional catch-all page')
expect(data5.params).toEqual({})
expect(isNaN(data5.random)).toBe(false)
expect(data5.random).not.toBe(data.random)
const html6 = await renderViaHTTP(
appPort,
'/catch-all/[[...rest]]',
{ nxtPrest: 'frank' },
{
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
)
const $6 = cheerio.load(html6)
const data6 = JSON.parse($6('#props').text())
expect($6('#catch-all').text()).toBe('optional catch-all page')
expect(data6.params).toEqual({ rest: ['frank'] })
expect(isNaN(data6.random)).toBe(false)
expect(data6.random).not.toBe(data.random)
})
describe('partial optional catch-all route', () => {
it.each([
{
path: '/partial-catch-all/hello.com',
query: { nxtPdomain: 'hello.com' },
expected: { domain: 'hello.com' },
},
{
path: '/partial-catch-all/hello.com/hello',
query: { nxtPdomain: 'hello.com', nxtPrest: 'hello' },
expected: { domain: 'hello.com', rest: ['hello'] },
},
{
path: '/partial-catch-all/hello.com/hello/world',
query: { nxtPdomain: 'hello.com', nxtPrest: 'hello/world' },
expected: { domain: 'hello.com', rest: ['hello', 'world'] },
},
])('should render $path', async ({ query, expected }) => {
const html = await renderViaHTTP(
appPort,
'/partial-catch-all/[domain]/[[...rest]]',
query,
{
headers: {
'x-matched-path': '/partial-catch-all/[domain]/[[...rest]]',
},
}
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#catch-all').text()).toBe('partial optional catch-all page')
expect(data.params).toEqual(expected)
expect(data.hello).toBe('world')
})
}) })
it('should return data correctly with x-matched-path for optional catch-all route', async () => { it('should return data correctly with x-matched-path for optional catch-all route', async () => {
......
...@@ -244,6 +244,42 @@ describe('required server files', () => { ...@@ -244,6 +244,42 @@ describe('required server files', () => {
} }
) )
it('should handle data routes with optional catch-all params', async () => {
let res = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/catch-all.json`,
{},
{
headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all.json`,
},
}
)
expect(res.status).toBe(200)
let json = await res.json()
expect(json.pageProps.params).toEqual({
rest: undefined,
})
res = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/catch-all/next.js.json`,
{},
{
headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all/next.js.json`,
},
}
)
expect(res.status).toBe(200)
json = await res.json()
expect(json.pageProps.params).toEqual({
rest: ['next.js'],
})
})
it.each([ it.each([
{ {
case: 'notFound no revalidate', case: 'notFound no revalidate',
......
...@@ -14483,6 +14483,9 @@ ...@@ -14483,6 +14483,9 @@
"Required Server Files production mode should render dynamic SSR page correctly", "Required Server Files production mode should render dynamic SSR page correctly",
"Required Server Files production mode should render dynamic SSR page correctly with x-matched-path", "Required Server Files production mode should render dynamic SSR page correctly with x-matched-path",
"Required Server Files production mode should render fallback optional catch-all route correctly with x-matched-path and routes-matches", "Required Server Files production mode should render fallback optional catch-all route correctly with x-matched-path and routes-matches",
"Required Server Files production mode partial optional catch-all route should render /partial-catch-all/hello.com",
"Required Server Files production mode partial optional catch-all route should render /partial-catch-all/hello.com/hello",
"Required Server Files production mode partial optional catch-all route should render /partial-catch-all/hello.com/hello/world",
"Required Server Files production mode should render fallback page correctly", "Required Server Files production mode should render fallback page correctly",
"Required Server Files production mode should render fallback page correctly with x-matched-path and routes-matches", "Required Server Files production mode should render fallback page correctly with x-matched-path and routes-matches",
"Required Server Files production mode should return data correctly with x-matched-path", "Required Server Files production mode should return data correctly with x-matched-path",
...@@ -16646,6 +16649,7 @@ ...@@ -16646,6 +16649,7 @@
"required server files should have correct cache-control for notFound with revalidate", "required server files should have correct cache-control for notFound with revalidate",
"required server files should have correct cache-control for redirect no revalidate", "required server files should have correct cache-control for redirect no revalidate",
"required server files should have correct cache-control for redirect with revalidate", "required server files should have correct cache-control for redirect with revalidate",
"required server files should handle data routes with optional catch-all params",
"required server files should have correct resolvedUrl from dynamic route", "required server files should have correct resolvedUrl from dynamic route",
"required server files should have correct resolvedUrl from rewrite", "required server files should have correct resolvedUrl from rewrite",
"required server files should have correct resolvedUrl from rewrite with added query", "required server files should have correct resolvedUrl from rewrite with added query",
...@@ -18641,6 +18641,9 @@ ...@@ -18641,6 +18641,9 @@
"Required Server Files production mode should render dynamic SSR page correctly", "Required Server Files production mode should render dynamic SSR page correctly",
"Required Server Files production mode should render dynamic SSR page correctly with x-matched-path", "Required Server Files production mode should render dynamic SSR page correctly with x-matched-path",
"Required Server Files production mode should render fallback optional catch-all route correctly with x-matched-path and routes-matches", "Required Server Files production mode should render fallback optional catch-all route correctly with x-matched-path and routes-matches",
"Required Server Files production mode partial optional catch-all route should render /partial-catch-all/hello.com",
"Required Server Files production mode partial optional catch-all route should render /partial-catch-all/hello.com/hello",
"Required Server Files production mode partial optional catch-all route should render /partial-catch-all/hello.com/hello/world",
"Required Server Files production mode should render fallback page correctly", "Required Server Files production mode should render fallback page correctly",
"Required Server Files production mode should render fallback page correctly with x-matched-path and routes-matches", "Required Server Files production mode should render fallback page correctly with x-matched-path and routes-matches",
"Required Server Files production mode should return data correctly with x-matched-path", "Required Server Files production mode should return data correctly with x-matched-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