Skip to content

Commit

Permalink
Correct query behavior for falsey values to pre 9.5.1 behavior (#16608)
Browse files Browse the repository at this point in the history
Fixes #16147
  • Loading branch information
Janpot authored Sep 10, 2020
1 parent ca986ab commit 5aed1d2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/next/next-server/lib/router/utils/querystring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ export function searchParamsToUrlQuery(
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(
urlQuery: ParsedUrlQuery
): URLSearchParams {
const result = new URLSearchParams()
Object.entries(urlQuery).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((item) => result.append(key, item))
value.forEach((item) => result.append(key, stringifyUrlQueryParam(item)))
} else {
result.set(key, value)
result.set(key, stringifyUrlQueryParam(value))
}
})
return result
Expand Down
2 changes: 1 addition & 1 deletion test/integration/build-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('Build Output', () => {
expect(parseFloat(err404FirstLoad) - 63.6).toBeLessThanOrEqual(0)
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)

if (_appSize.endsWith('kB')) {
Expand Down
43 changes: 43 additions & 0 deletions test/integration/client-navigation/pages/nav/query-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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>
</>
)
}
6 changes: 6 additions & 0 deletions test/integration/client-navigation/pages/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useRouter } from 'next/router'

export default function Page() {
const router = useRouter()
return <pre id="query-value">{JSON.stringify(router.query, null, 2)}</pre>
}
20 changes: 20 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,26 @@ describe('Client Navigation', () => {

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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/size-limit/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('Production response size', () => {
)

// 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).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
})
Expand Down

0 comments on commit 5aed1d2

Please sign in to comment.