@@ -90,11 +90,15 @@ export class URL {
9090 }
9191 }
9292
93+ // Only add trailing slash if URL has no path (just domain)
9394 if (
9495 ! this . _url . endsWith ( '/' ) &&
9596 ! ( this . _url . includes ( '?' ) || this . _url . includes ( '#' ) )
9697 ) {
97- this . _url += '/' ;
98+ const afterProtocol = this . _url . split ( '://' ) [ 1 ] ;
99+ if ( afterProtocol && ! afterProtocol . includes ( '/' ) ) {
100+ this . _url += '/' ;
101+ }
98102 }
99103 } else {
100104 if ( typeof base === 'string' ) {
@@ -170,6 +174,24 @@ export class URL {
170174 return searchMatch ? `?${ searchMatch [ 1 ] } ` : '' ;
171175 }
172176
177+ set search ( value : string ) {
178+ // Remove leading '?' if present
179+ const searchString = value . startsWith ( '?' ) ? value . slice ( 1 ) : value ;
180+
181+ // Update the internal URL
182+ const baseUrl = this . _url . split ( '?' ) [ 0 ] . split ( '#' ) [ 0 ] ;
183+ const hash = this . hash ;
184+
185+ if ( searchString ) {
186+ this . _url = baseUrl + '?' + searchString + hash ;
187+ } else {
188+ this . _url = baseUrl + hash ;
189+ }
190+
191+ // Reset the searchParams instance so it gets recreated with new values
192+ this . _searchParamsInstance = null ;
193+ }
194+
173195 get searchParams ( ) : URLSearchParams {
174196 if ( this . _searchParamsInstance == null ) {
175197 this . _searchParamsInstance = new URLSearchParams ( this . search ) ;
@@ -185,10 +207,19 @@ export class URL {
185207 if ( this . _searchParamsInstance === null ) {
186208 return this . _url ;
187209 }
210+
211+ // Remove existing search params and hash from the URL
212+ const baseUrl = this . _url . split ( '?' ) [ 0 ] . split ( '#' ) [ 0 ] ;
213+ const hash = this . hash ;
214+
188215 // $FlowFixMe[incompatible-use]
189216 const instanceString = this . _searchParamsInstance . toString ( ) ;
190- const separator = this . _url . indexOf ( '?' ) > - 1 ? '&' : '?' ;
191- return this . _url + separator + instanceString ;
217+
218+ if ( instanceString ) {
219+ return baseUrl + '?' + instanceString + hash ;
220+ } else {
221+ return baseUrl + hash ;
222+ }
192223 }
193224
194225 get username ( ) : string {
0 commit comments