Unverified Commit 827fb90b authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

chore: break calls to forEach into for loops (#74523)

This breaks a few functions down that were using array methods before to
use for loops instead. Reducing the amount of functions created should
improve performance a tiny bit.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 286c14d0
......@@ -1160,18 +1160,18 @@ export default abstract class Server<
if (didRewrite && parsedUrl.pathname) {
addRequestMeta(req, 'rewroteURL', parsedUrl.pathname)
}
const routeParamKeys = new Set<string>()
for (const key of Object.keys(parsedUrl.query)) {
const value = parsedUrl.query[key]
normalizeNextQueryParam(key, (normalizedKey) => {
if (!parsedUrl) return // typeguard
// Normalize all the query params to remove the prefixes.
const routeParamKeys = new Set<string>()
for (const [key, value] of Object.entries(parsedUrl.query)) {
if (typeof value === 'undefined') continue
const normalizedKey = normalizeNextQueryParam(key)
if (normalizedKey) {
parsedUrl.query[normalizedKey] = value
routeParamKeys.add(normalizedKey)
delete parsedUrl.query[key]
})
}
}
// interpolate dynamic params and normalize URL if needed
......
......@@ -110,14 +110,14 @@ export async function adapter(
for (const key of keys) {
const value = requestUrl.searchParams.getAll(key)
normalizeNextQueryParam(key, (normalizedKey) => {
const normalizedKey = normalizeNextQueryParam(key)
if (normalizedKey) {
requestUrl.searchParams.delete(normalizedKey)
for (const val of value) {
requestUrl.searchParams.append(normalizedKey, val)
}
requestUrl.searchParams.delete(key)
})
}
}
// Ensure users only see page requests, never data requests.
......
......@@ -153,18 +153,15 @@ export function validateURL(url: string | URL): string {
/**
* Normalizes `nxtP` and `nxtI` query param values to remove the prefix.
* This function does not mutate the input key; it calls the provided function
* with the normalized key.
* This function does not mutate the input key.
*/
export function normalizeNextQueryParam(
key: string,
onKeyNormalized: (normalizedKey: string) => void
) {
export function normalizeNextQueryParam(key: string): null | string {
const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX]
for (const prefix of prefixes) {
if (key !== prefix && key.startsWith(prefix)) {
const normalizedKey = key.substring(prefix.length)
onKeyNormalized(normalizedKey)
return normalizedKey
}
}
return null
}
......@@ -4,21 +4,25 @@ export function searchParamsToUrlQuery(
searchParams: URLSearchParams
): ParsedUrlQuery {
const query: ParsedUrlQuery = {}
searchParams.forEach((value, key) => {
if (typeof query[key] === 'undefined') {
for (const [key, value] of searchParams.entries()) {
const existing = query[key]
if (typeof existing === 'undefined') {
query[key] = value
} else if (Array.isArray(query[key])) {
;(query[key] as string[]).push(value)
} else if (Array.isArray(existing)) {
existing.push(value)
} else {
query[key] = [query[key] as string, value]
query[key] = [existing, value]
}
})
}
return query
}
function stringifyUrlQueryParam(param: unknown): string {
if (typeof param === 'string') {
return param
}
if (
typeof param === 'string' ||
(typeof param === 'number' && !isNaN(param)) ||
typeof param === 'boolean'
) {
......@@ -28,27 +32,33 @@ function stringifyUrlQueryParam(param: unknown): string {
}
}
export function urlQueryToSearchParams(
urlQuery: ParsedUrlQuery
): URLSearchParams {
const result = new URLSearchParams()
Object.entries(urlQuery).forEach(([key, value]) => {
export function urlQueryToSearchParams(query: ParsedUrlQuery): URLSearchParams {
const searchParams = new URLSearchParams()
for (const [key, value] of Object.entries(query)) {
if (Array.isArray(value)) {
value.forEach((item) => result.append(key, stringifyUrlQueryParam(item)))
for (const item of value) {
searchParams.append(key, stringifyUrlQueryParam(item))
}
} else {
result.set(key, stringifyUrlQueryParam(value))
searchParams.set(key, stringifyUrlQueryParam(value))
}
})
return result
}
return searchParams
}
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))
})
for (const searchParams of searchParamsList) {
for (const key of searchParams.keys()) {
target.delete(key)
}
for (const [key, value] of searchParams.entries()) {
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