Skip to content

Commit

Permalink
Remove querystring from the client (#15378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Aug 6, 2020
1 parent 882288b commit cbfb8cb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 27 deletions.
12 changes: 7 additions & 5 deletions packages/next/client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global location */
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 ReactDOM from 'react-dom'
import { HeadManagerContext } from '../next-server/lib/head-manager-context'
Expand Down Expand Up @@ -99,10 +99,12 @@ class Container extends React.Component {
router.replace(
router.pathname +
'?' +
stringifyQs({
...router.query,
...parseQs(location.search.substr(1)),
}),
String(
querystring.assign(
querystring.urlQueryToSearchParams(router.query),
new URLSearchParams(location.search)
)
),
asPath,
{
// WARNING: `_h` is an internal option for handing Next.js
Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mitt from '../next-server/lib/mitt'
import { isDynamicRoute } from './../next-server/lib/router/utils/is-dynamic'
import { getRouteMatcher } from './../next-server/lib/router/utils/route-matcher'
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 escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters'
import getAssetPathFromRoute from './../next-server/lib/router/utils/get-asset-path-from-route'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { isDynamicRoute } from './utils/is-dynamic'
import { getRouteMatcher } from './utils/route-matcher'
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 {
removePathTrailingSlash,
Expand Down
6 changes: 3 additions & 3 deletions packages/next/next-server/lib/router/utils/format-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

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/

Expand All @@ -45,8 +46,7 @@ export function formatUrl(urlObj: UrlObject) {
}

if (query && typeof query === 'object') {
// query = '' + new URLSearchParams(query);
query = encodeQuerystring(query)
query = String(querystring.urlQueryToSearchParams(query as ParsedUrlQuery))
}

let search = urlObj.search || (query && `?${query}`) || ''
Expand Down
42 changes: 42 additions & 0 deletions packages/next/next-server/lib/router/utils/querystring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ParsedUrlQuery } from 'querystring'

export function searchParamsToUrlQuery(
searchParams: URLSearchParams
): ParsedUrlQuery {
const query: ParsedUrlQuery = {}
searchParams.forEach((value, key) => {
if (typeof query[key] === 'undefined') {
query[key] = value
} else if (Array.isArray(query[key])) {
;(query[key] as string[]).push(value)
} else {
query[key] = [query[key] as string, value]
}
})
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
}

This file was deleted.

0 comments on commit cbfb8cb

Please sign in to comment.