Unverified Commit f841307f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix parsing params for i18n optional route in minimal mode (#33896)

This fixes our parsing of params with i18n for optional catch-all routes and ensures a regression test catches this case.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/30631
parent 7b1bacee
...@@ -179,10 +179,11 @@ export function getUtils({ ...@@ -179,10 +179,11 @@ export function getUtils({
// Simulate a RegExp match from the \`req.url\` input // Simulate a RegExp match from the \`req.url\` input
exec: (str: string) => { exec: (str: string) => {
const obj = parseQs(str) const obj = parseQs(str)
const matchesHasLocale =
i18n && detectedLocale && obj['1'] === detectedLocale
// favor named matches if available // favor named matches if available
const routeKeyNames = Object.keys(routeKeys || {}) const routeKeyNames = Object.keys(routeKeys || {})
const filterLocaleItem = (val: string | string[]) => { const filterLocaleItem = (val: string | string[]) => {
if (i18n) { if (i18n) {
// locale items can be included in route-matches // locale items can be included in route-matches
...@@ -228,8 +229,13 @@ export function getUtils({ ...@@ -228,8 +229,13 @@ export function getUtils({
return Object.keys(obj).reduce((prev, key) => { return Object.keys(obj).reduce((prev, key) => {
if (!filterLocaleItem(obj[key])) { if (!filterLocaleItem(obj[key])) {
let normalizedKey = key
if (matchesHasLocale) {
normalizedKey = parseInt(key, 10) - 1 + ''
}
return Object.assign(prev, { return Object.assign(prev, {
[key]: obj[key], [normalizedKey]: obj[key],
}) })
} }
return prev return prev
......
...@@ -601,11 +601,12 @@ describe('should set-up next', () => { ...@@ -601,11 +601,12 @@ describe('should set-up next', () => {
it('should normalize optional values correctly for SSG page', async () => { it('should normalize optional values correctly for SSG page', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
'/optional-ssg', '/en/optional-ssg/[[...rest]]',
{ rest: '', another: 'value' }, undefined,
{ {
headers: { headers: {
'x-matched-path': '/optional-ssg/[[...rest]]', 'x-matched-path': '/en/optional-ssg/[[...rest]]',
'x-now-route-matches': 'nextLocale=en&1=en',
}, },
} }
) )
...@@ -616,6 +617,28 @@ describe('should set-up next', () => { ...@@ -616,6 +617,28 @@ describe('should set-up next', () => {
expect(props.params).toEqual({}) expect(props.params).toEqual({})
}) })
it('should normalize optional values correctly for nested optional SSG page', async () => {
const res = await fetchViaHTTP(
appPort,
'/en/[slug]/social/[[...rest]]',
undefined,
{
headers: {
'x-matched-path': '/en/[slug]/social/[[...rest]]',
'x-now-route-matches': 'nextLocale=en&1=en&2=user-123&slug=user-123',
},
}
)
const html = await res.text()
const $ = cheerio.load(html)
const props = JSON.parse($('#props').text())
expect(props.params).toEqual({
slug: 'user-123',
})
expect($('#page').text()).toBe('/[slug]/social/[[...rest]]')
})
it('should normalize optional values correctly for SSG page with encoded slash', async () => { it('should normalize optional values correctly for SSG page with encoded slash', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
......
export const getStaticProps = ({ params }) => {
return {
props: {
random: Math.random(),
params: params || null,
},
revalidate: 1,
}
}
export const getStaticPaths = () => {
return {
paths: [],
fallback: true,
}
}
export default function Page(props) {
return (
<>
<p id="page">/[slug]/social/[[...rest]]</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
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