11import type { IncomingHttpHeaders } from 'node:http'
2- import { parse , type UrlWithParsedQuery } from 'node:url '
2+ import type { ParsedUrlQuery } from 'querystring '
33import { match } from 'next/dist/compiled/path-to-regexp'
44import {
55 matchHas ,
@@ -20,6 +20,7 @@ import type { BaseNextRequest } from '../../../server/base-http'
2020import type { Params } from '../../../server/request/params'
2121import { constructRequest } from './utils'
2222import { 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/
2930function 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