Unverified Commit 5aed1d2e authored by Jan Potoms's avatar Jan Potoms Committed by GitHub

Correct query behavior for falsey values to pre 9.5.1 behavior (#16608)

Fixes https://github.com/vercel/next.js/issues/16147
parent ca986abd
...@@ -16,15 +16,27 @@ export function searchParamsToUrlQuery( ...@@ -16,15 +16,27 @@ export function searchParamsToUrlQuery(
return query return query
} }
function stringifyUrlQueryParam(param: string): string {
if (
typeof param === 'string' ||
(typeof param === 'number' && !isNaN(param)) ||
typeof param === 'boolean'
) {
return String(param)
} else {
return ''
}
}
export function urlQueryToSearchParams( export function urlQueryToSearchParams(
urlQuery: ParsedUrlQuery urlQuery: ParsedUrlQuery
): URLSearchParams { ): URLSearchParams {
const result = new URLSearchParams() const result = new URLSearchParams()
Object.entries(urlQuery).forEach(([key, value]) => { Object.entries(urlQuery).forEach(([key, value]) => {
if (Array.isArray(value)) { if (Array.isArray(value)) {
value.forEach((item) => result.append(key, item)) value.forEach((item) => result.append(key, stringifyUrlQueryParam(item)))
} else { } else {
result.set(key, value) result.set(key, stringifyUrlQueryParam(value))
} }
}) })
return result return result
......
...@@ -104,7 +104,7 @@ describe('Build Output', () => { ...@@ -104,7 +104,7 @@ describe('Build Output', () => {
expect(parseFloat(err404FirstLoad) - 63.6).toBeLessThanOrEqual(0) expect(parseFloat(err404FirstLoad) - 63.6).toBeLessThanOrEqual(0)
expect(err404FirstLoad.endsWith('kB')).toBe(true) expect(err404FirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(sharedByAll) - 60.1).toBeLessThanOrEqual(0) expect(parseFloat(sharedByAll) - 60.2).toBeLessThanOrEqual(0)
expect(sharedByAll.endsWith('kB')).toBe(true) expect(sharedByAll.endsWith('kB')).toBe(true)
if (_appSize.endsWith('kB')) { if (_appSize.endsWith('kB')) {
......
import { useRouter } from 'next/router'
export default function Page() {
const router = useRouter()
return (
<>
<button
id="click-me"
onClick={() =>
router.push({
pathname: '/query',
query: {
param1: '',
param2: undefined,
param3: null,
param4: 0,
param5: false,
param6: [],
param7: {},
param8: NaN,
param9: new Date(1234),
param10: /hello/,
param11: [
'',
undefined,
null,
0,
false,
[],
{},
NaN,
new Date(1234),
/hello/,
],
},
})
}
>
Click me
</button>
</>
)
}
import { useRouter } from 'next/router'
export default function Page() {
const router = useRouter()
return <pre id="query-value">{JSON.stringify(router.query, null, 2)}</pre>
}
...@@ -801,6 +801,26 @@ describe('Client Navigation', () => { ...@@ -801,6 +801,26 @@ describe('Client Navigation', () => {
await browser.close() await browser.close()
}) })
it('should handle undefined in router.push', async () => {
const browser = await webdriver(context.appPort, '/nav/query-params')
await browser.elementByCss('#click-me').click()
const query = JSON.parse(
await browser.waitForElementByCss('#query-value').text()
)
expect(query).toEqual({
param1: '',
param2: '',
param3: '',
param4: '0',
param5: 'false',
param7: '',
param8: '',
param9: '',
param10: '',
param11: ['', '', '', '0', 'false', '', '', '', '', ''],
})
})
}) })
describe('with querystring relative urls', () => { describe('with querystring relative urls', () => {
......
...@@ -100,7 +100,7 @@ describe('Production response size', () => { ...@@ -100,7 +100,7 @@ describe('Production response size', () => {
) )
// These numbers are without gzip compression! // These numbers are without gzip compression!
const delta = responseSizesBytes - 169 * 1024 const delta = responseSizesBytes - 170 * 1024
expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb
expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
}) })
......
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