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<
let params: ParsedUrlQuery | false = {}
let paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query
parsedUrl.query,
false
)
// for prerendered ISR paths we attempt parsing the route
......@@ -1196,7 +1197,7 @@ export default abstract class Server<
let matcherParams = utils.dynamicRouteMatcher?.(normalizedUrlPath)
if (matcherParams) {
utils.normalizeDynamicRouteParams(matcherParams)
utils.normalizeDynamicRouteParams(matcherParams, false)
Object.assign(paramsResult.params, matcherParams)
paramsResult.hasValidParams = true
}
......@@ -1218,8 +1219,10 @@ export default abstract class Server<
let matcherParams = utils.dynamicRouteMatcher?.(matchedPath)
if (matcherParams) {
const curParamsResult =
utils.normalizeDynamicRouteParams(matcherParams)
const curParamsResult = utils.normalizeDynamicRouteParams(
matcherParams,
false
)
if (curParamsResult.hasValidParams) {
Object.assign(params, matcherParams)
......@@ -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
if (
utils.defaultRouteMatches &&
......@@ -1276,7 +1292,7 @@ export default abstract class Server<
}
if (pageIsDynamic || didRewrite) {
utils.normalizeVercelUrl(req, true, [
utils.normalizeVercelUrl(req, [
...rewriteParamKeys,
...Object.keys(utils.defaultRouteRegex?.groups || {}),
])
......
......@@ -24,36 +24,34 @@ import { normalizeNextQueryParam } from './web/utils'
export function normalizeVercelUrl(
req: BaseNextRequest,
trustQuery: boolean,
paramKeys?: string[],
pageIsDynamic?: boolean,
defaultRouteRegex?: ReturnType<typeof getNamedRouteRegex> | undefined
paramKeys: string[],
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
) {
if (!defaultRouteRegex) return
// make sure to normalize req.url on Vercel to strip dynamic params
// from the query which are added during routing
if (pageIsDynamic && trustQuery && defaultRouteRegex) {
const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search
for (const key of Object.keys(_parsedUrl.query)) {
const isNextQueryPrefix =
key !== NEXT_QUERY_PARAM_PREFIX &&
key.startsWith(NEXT_QUERY_PARAM_PREFIX)
const isNextInterceptionMarkerPrefix =
key !== NEXT_INTERCEPTION_MARKER_PREFIX &&
key.startsWith(NEXT_INTERCEPTION_MARKER_PREFIX)
if (
isNextQueryPrefix ||
isNextInterceptionMarkerPrefix ||
(paramKeys || Object.keys(defaultRouteRegex.groups)).includes(key)
) {
delete _parsedUrl.query[key]
}
const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search
for (const key of Object.keys(_parsedUrl.query)) {
const isNextQueryPrefix =
key !== NEXT_QUERY_PARAM_PREFIX && key.startsWith(NEXT_QUERY_PARAM_PREFIX)
const isNextInterceptionMarkerPrefix =
key !== NEXT_INTERCEPTION_MARKER_PREFIX &&
key.startsWith(NEXT_INTERCEPTION_MARKER_PREFIX)
if (
isNextQueryPrefix ||
isNextInterceptionMarkerPrefix ||
(paramKeys || Object.keys(defaultRouteRegex.groups)).includes(key)
) {
delete _parsedUrl.query[key]
}
req.url = formatUrl(_parsedUrl)
}
req.url = formatUrl(_parsedUrl)
}
export function interpolateDynamicPath(
......@@ -89,27 +87,21 @@ export function interpolateDynamicPath(
}
export function normalizeDynamicRouteParams(
params: ParsedUrlQuery,
ignoreOptional?: boolean,
defaultRouteRegex?: ReturnType<typeof getNamedRouteRegex> | undefined,
defaultRouteMatches?: ParsedUrlQuery | undefined
query: ParsedUrlQuery,
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex>,
defaultRouteMatches: ParsedUrlQuery,
ignoreMissingOptional: boolean
) {
let hasValidParams = true
if (!defaultRouteRegex) return { params, hasValidParams: false }
let params: ParsedUrlQuery = {}
params = Object.keys(defaultRouteRegex.groups).reduce((prev, key) => {
let value: string | string[] | undefined = params[key]
for (const key of Object.keys(defaultRouteRegex.groups)) {
let value: string | string[] | undefined = query[key]
if (typeof value === 'string') {
value = normalizeRscURL(value)
}
if (Array.isArray(value)) {
value = value.map((val) => {
if (typeof val === 'string') {
val = normalizeRscURL(val)
}
return val
})
} else if (Array.isArray(value)) {
value = value.map(normalizeRscURL)
}
// if the value matches the default value we can't rely
......@@ -128,9 +120,9 @@ export function normalizeDynamicRouteParams(
if (
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
......@@ -145,7 +137,7 @@ export function normalizeDynamicRouteParams(
(value[0] === 'index' || value[0] === `[[...${key}]]`)))
) {
value = undefined
delete params[key]
delete query[key]
}
// query values from the proxy aren't already split into arrays
......@@ -159,10 +151,9 @@ export function normalizeDynamicRouteParams(
}
if (value) {
prev[key] = value
params[key] = value
}
return prev
}, {} as ParsedUrlQuery)
}
return {
params,
......@@ -375,28 +366,30 @@ export function getUtils({
dynamicRouteMatcher,
defaultRouteMatches,
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: (
params: ParsedUrlQuery,
ignoreOptional?: boolean
) =>
normalizeDynamicRouteParams(
params,
ignoreOptional,
query: ParsedUrlQuery,
ignoreMissingOptional: boolean
) => {
if (!defaultRouteRegex || !defaultRouteMatches) {
return { params: {}, hasValidParams: false }
}
return normalizeDynamicRouteParams(
query,
defaultRouteRegex,
defaultRouteMatches
),
normalizeVercelUrl: (
req: BaseNextRequest,
trustQuery: boolean,
paramKeys?: string[]
) =>
normalizeVercelUrl(
req,
trustQuery,
paramKeys,
pageIsDynamic,
defaultRouteRegex
),
defaultRouteMatches,
ignoreMissingOptional
)
},
normalizeVercelUrl: (req: BaseNextRequest, paramKeys: string[]) =>
normalizeVercelUrl(req, paramKeys, defaultRouteRegex),
interpolateDynamicPath: (
pathname: string,
params: Record<string, undefined | string | string[]>
......
......@@ -173,9 +173,9 @@ export default class NextWebServer extends BaseServer<
) as NextParsedUrlQuery
const paramsResult = normalizeDynamicRouteParams(
query,
false,
routeRegex,
defaultRouteMatches
defaultRouteMatches,
false
)
const normalizedParams = paramsResult.hasValidParams
? paramsResult.params
......@@ -186,13 +186,7 @@ export default class NextWebServer extends BaseServer<
normalizedParams,
routeRegex
)
normalizeVercelUrl(
req,
true,
Object.keys(routeRegex.routeKeys),
true,
routeRegex
)
normalizeVercelUrl(req, Object.keys(routeRegex.routeKeys), routeRegex)
}
}
......
......@@ -82,7 +82,8 @@ export class EdgeRouteModuleWrapper {
})
const { params } = utils.normalizeDynamicRouteParams(
searchParamsToUrlQuery(request.nextUrl.searchParams)
searchParamsToUrlQuery(request.nextUrl.searchParams),
false
)
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', () => {
{
headers: {
'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', () => {
const html3 = await renderViaHTTP(
appPort,
'/catch-all/[[..rest]]',
'/catch-all/[[...rest]]',
undefined,
{
headers: {
......@@ -359,6 +359,98 @@ describe('Required Server Files', () => {
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
expect(isNaN(data3.random)).toBe(false)
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 () => {
......
......@@ -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([
{
case: 'notFound no revalidate',
......
......@@ -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 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 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 with x-matched-path and routes-matches",
"Required Server Files production mode should return data correctly with x-matched-path",
......@@ -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 redirect no 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 rewrite",
"required server files should have correct resolvedUrl from rewrite with added query",
......@@ -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 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 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 with x-matched-path and routes-matches",
"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