Unverified Commit cbfb8cbc authored by Jan Potoms's avatar Jan Potoms Committed by GitHub

Remove querystring from the client (#15378)

parent 882288b5
/* global location */ /* global location */
import { createRouter, makePublicRouterInstance } from 'next/router' import { createRouter, makePublicRouterInstance } from 'next/router'
import { parse as parseQs, stringify as stringifyQs } from 'querystring' import * as querystring from '../next-server/lib/router/utils/querystring'
import React from 'react' import React from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import { HeadManagerContext } from '../next-server/lib/head-manager-context' import { HeadManagerContext } from '../next-server/lib/head-manager-context'
...@@ -99,10 +99,12 @@ class Container extends React.Component { ...@@ -99,10 +99,12 @@ class Container extends React.Component {
router.replace( router.replace(
router.pathname + router.pathname +
'?' + '?' +
stringifyQs({ String(
...router.query, querystring.assign(
...parseQs(location.search.substr(1)), querystring.urlQueryToSearchParams(router.query),
}), new URLSearchParams(location.search)
)
),
asPath, asPath,
{ {
// WARNING: `_h` is an internal option for handing Next.js // WARNING: `_h` is an internal option for handing Next.js
......
...@@ -2,7 +2,7 @@ import mitt from '../next-server/lib/mitt' ...@@ -2,7 +2,7 @@ import mitt from '../next-server/lib/mitt'
import { isDynamicRoute } from './../next-server/lib/router/utils/is-dynamic' import { isDynamicRoute } from './../next-server/lib/router/utils/is-dynamic'
import { getRouteMatcher } from './../next-server/lib/router/utils/route-matcher' import { getRouteMatcher } from './../next-server/lib/router/utils/route-matcher'
import { getRouteRegex } from './../next-server/lib/router/utils/route-regex' import { getRouteRegex } from './../next-server/lib/router/utils/route-regex'
import { searchParamsToUrlQuery } from './../next-server/lib/router/utils/search-params-to-url-query' import { searchParamsToUrlQuery } from './../next-server/lib/router/utils/querystring'
import { parseRelativeUrl } from './../next-server/lib/router/utils/parse-relative-url' import { parseRelativeUrl } from './../next-server/lib/router/utils/parse-relative-url'
import escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters' import escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters'
import getAssetPathFromRoute from './../next-server/lib/router/utils/get-asset-path-from-route' import getAssetPathFromRoute from './../next-server/lib/router/utils/get-asset-path-from-route'
......
...@@ -16,7 +16,7 @@ import { ...@@ -16,7 +16,7 @@ import {
import { isDynamicRoute } from './utils/is-dynamic' import { isDynamicRoute } from './utils/is-dynamic'
import { getRouteMatcher } from './utils/route-matcher' import { getRouteMatcher } from './utils/route-matcher'
import { getRouteRegex } from './utils/route-regex' import { getRouteRegex } from './utils/route-regex'
import { searchParamsToUrlQuery } from './utils/search-params-to-url-query' import { searchParamsToUrlQuery } from './utils/querystring'
import { parseRelativeUrl } from './utils/parse-relative-url' import { parseRelativeUrl } from './utils/parse-relative-url'
import { import {
removePathTrailingSlash, removePathTrailingSlash,
......
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
import { UrlObject } from 'url' import { UrlObject } from 'url'
import { encode as encodeQuerystring } from 'querystring' import { ParsedUrlQuery } from 'querystring'
import * as querystring from './querystring'
const slashedProtocols = /https?|ftp|gopher|file/ const slashedProtocols = /https?|ftp|gopher|file/
...@@ -45,8 +46,7 @@ export function formatUrl(urlObj: UrlObject) { ...@@ -45,8 +46,7 @@ export function formatUrl(urlObj: UrlObject) {
} }
if (query && typeof query === 'object') { if (query && typeof query === 'object') {
// query = '' + new URLSearchParams(query); query = String(querystring.urlQueryToSearchParams(query as ParsedUrlQuery))
query = encodeQuerystring(query)
} }
let search = urlObj.search || (query && `?${query}`) || '' let search = urlObj.search || (query && `?${query}`) || ''
......
...@@ -15,3 +15,28 @@ export function searchParamsToUrlQuery( ...@@ -15,3 +15,28 @@ export function searchParamsToUrlQuery(
}) })
return query return query
} }
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))
} else {
result.set(key, value)
}
})
return result
}
export function assign(
target: URLSearchParams,
...searchParamsList: URLSearchParams[]
): URLSearchParams {
searchParamsList.forEach((searchParams) => {
Array.from(searchParams.keys()).forEach((key) => target.delete(key))
searchParams.forEach((value, key) => target.append(key, value))
})
return 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