Skip to content

Commit da6e8b6

Browse files
authored
[testmode] Stop using deprecated url.parse (#98194)
`unstable_getResponseFromNextConfig` parsed the request URL with the deprecated `url.parse()` (DEP0169) This change constructs the same pathname/query/protocol/host shape from the WHATWG `URL` API instead, keeping `protocol` and `host` null for relative URLs so the existing rewrite base fallback keeps working. Query parsing goes through the existing `searchParamsToUrlQuery` helper, which decodes `+` as a space where the legacy parser left it untouched; we accept that edge-case difference in this testing utility.
1 parent dcbfff7 commit da6e8b6

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

‎packages/next/src/experimental/testing/server/config-testing-utils.ts‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IncomingHttpHeaders } from 'node:http'
2-
import { parse, type UrlWithParsedQuery } from 'node:url'
2+
import type { ParsedUrlQuery } from 'querystring'
33
import { match } from 'next/dist/compiled/path-to-regexp'
44
import {
55
matchHas,
@@ -20,6 +20,7 @@ import type { BaseNextRequest } from '../../../server/base-http'
2020
import type { Params } from '../../../server/request/params'
2121
import { constructRequest } from './utils'
2222
import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/helpers/parsed-url-query-to-params'
23+
import { searchParamsToUrlQuery } from '../../../shared/lib/router/utils/querystring'
2324

2425
/**
2526
* Tries to match the current request against the provided route. If there is
@@ -29,13 +30,10 @@ import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/
2930
function matchRoute(
3031
route: ManifestHeaderRoute | ManifestRedirectRoute | ManifestRewriteRoute,
3132
request: BaseNextRequest,
32-
parsedUrl: UrlWithParsedQuery
33+
pathname: string,
34+
query: ParsedUrlQuery
3335
): Params | undefined {
34-
const pathname = parsedUrl.pathname
35-
if (!pathname) {
36-
return
37-
}
38-
const regexMatches = pathname?.match(route.regex)
36+
const regexMatches = pathname.match(route.regex)
3937

4038
if (regexMatches) {
4139
const pathMatch = match<Params>(route.source)(pathname)
@@ -45,7 +43,7 @@ function matchRoute(
4543
)
4644
}
4745
if (route.has || route.missing) {
48-
if (!matchHas(request, parsedUrl.query, route.has, route.missing)) {
46+
if (!matchHas(request, query, route.has, route.missing)) {
4947
return
5048
}
5149
}
@@ -88,7 +86,9 @@ export async function unstable_getResponseFromNextConfig({
8886
headers?: IncomingHttpHeaders
8987
cookies?: Record<string, string>
9088
}): Promise<NextResponse> {
91-
const parsedUrl = parse(url, true)
89+
const parsed = new URL(url, 'https://example.com')
90+
const pathname = parsed.pathname
91+
const query = searchParamsToUrlQuery(parsed.searchParams)
9292
const request = constructRequest({ url, headers, cookies })
9393
const resolvedConfig = await normalizeConfig(
9494
PHASE_PRODUCTION_BUILD,
@@ -110,7 +110,7 @@ export async function unstable_getResponseFromNextConfig({
110110

111111
const respHeaders: Record<string, string> = {}
112112
for (const route of headerRoutes) {
113-
const matched = matchRoute(route, request, parsedUrl)
113+
const matched = matchRoute(route, request, pathname, query)
114114
if (matched) {
115115
for (const header of route.headers) {
116116
respHeaders[header.key] = header.value
@@ -120,15 +120,15 @@ export async function unstable_getResponseFromNextConfig({
120120
function matchRouteAndGetDestination(
121121
route: ManifestRedirectRoute | ManifestRewriteRoute
122122
): URL | undefined {
123-
const params = matchRoute(route, request, parsedUrl)
123+
const params = matchRoute(route, request, pathname, query)
124124
if (!params) {
125125
return
126126
}
127127
const { newUrl, parsedDestination } = prepareDestination({
128128
appendParamsToQuery: false,
129129
destination: route.destination,
130130
params,
131-
query: parsedUrl.query,
131+
query,
132132
})
133133
const searchParams = new URLSearchParams(
134134
parsedUrlQueryToParams(parsedDestination.query) as Record<string, string>
@@ -137,9 +137,7 @@ export async function unstable_getResponseFromNextConfig({
137137
searchParams.size > 0 ? `${newUrl}?${searchParams.toString()}` : newUrl,
138138
parsedDestination.hostname
139139
? `${parsedDestination.protocol}//${parsedDestination.hostname}`
140-
: parsedUrl.host
141-
? `${parsedUrl.protocol}//${parsedUrl.host}`
142-
: 'https://example.com'
140+
: parsed.origin
143141
)
144142
}
145143
for (const route of redirectRoutes) {

0 commit comments

Comments
 (0)