@@ -31,7 +31,7 @@ use std::{
3131 fs:: { OpenOptions , create_dir_all, read_to_string, remove_file} ,
3232 future:: Future ,
3333 io:: Write as _,
34- net:: { AddrParseError , IpAddr , Ipv4Addr , SocketAddr } ,
34+ net:: { AddrParseError , IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr } ,
3535 path:: { Path , PathBuf } ,
3636 pin:: Pin ,
3737 process:: { ExitStatus , exit} ,
@@ -228,6 +228,15 @@ struct DevUrlConfig {
228228 no_dev_server_wait : bool ,
229229}
230230
231+ fn is_localhost_url ( url : & url:: Url ) -> bool {
232+ match url. host ( ) {
233+ Some ( url:: Host :: Domain ( d) ) => d == "localhost" ,
234+ Some ( url:: Host :: Ipv4 ( i) ) => i == Ipv4Addr :: LOCALHOST || i == Ipv4Addr :: UNSPECIFIED ,
235+ Some ( url:: Host :: Ipv6 ( i) ) => i == Ipv6Addr :: LOCALHOST || i == Ipv6Addr :: UNSPECIFIED ,
236+ None => false ,
237+ }
238+ }
239+
231240fn use_network_address_for_dev_url (
232241 config : & mut ConfigMetadata ,
233242 dev_options : & mut crate :: dev:: Options ,
@@ -237,13 +246,7 @@ fn use_network_address_for_dev_url(
237246 let mut dev_url = config. build . dev_url . clone ( ) ;
238247
239248 let ip = if let Some ( url) = & mut dev_url {
240- let localhost = match url. host ( ) {
241- Some ( url:: Host :: Domain ( d) ) => d == "localhost" ,
242- Some ( url:: Host :: Ipv4 ( i) ) => i == Ipv4Addr :: LOCALHOST || i == Ipv4Addr :: UNSPECIFIED ,
243- _ => false ,
244- } ;
245-
246- if localhost {
249+ if is_localhost_url ( url) {
247250 let ip = dev_options
248251 . host
249252 . unwrap_or_else ( || * local_ip_address ( force_ip_prompt) ) ;
@@ -252,14 +255,10 @@ fn use_network_address_for_dev_url(
252255 "If your frontend is not listening on that address, try configuring your development server to use the `TAURI_DEV_HOST` environment variable or 0.0.0.0 as host"
253256 ) ;
254257
255- let url_str = format ! (
256- "{}://{}{}" ,
257- url. scheme( ) ,
258- SocketAddr :: new( ip, url. port_or_known_default( ) . unwrap( ) ) ,
259- url. path( )
260- ) ;
261- * url =
262- url:: Url :: parse ( & url_str) . with_context ( || format ! ( "failed to parse URL: {url_str}" ) ) ?;
258+ // only replace the host so the port, path, query and fragment are preserved
259+ if url. set_ip_host ( ip) . is_err ( ) {
260+ crate :: error:: bail!( "failed to set the host of {url} to {ip}" ) ;
261+ }
263262
264263 dev_options
265264 . config
@@ -814,6 +813,43 @@ fn log_finished(outputs: Vec<PathBuf>, kind: &str) {
814813mod tests {
815814 use super :: * ;
816815
816+ use std:: net:: { IpAddr , Ipv4Addr } ;
817+
818+ #[ test]
819+ fn detects_localhost_dev_urls ( ) {
820+ for url in [
821+ "http://localhost:1420" ,
822+ "http://127.0.0.1:1420" ,
823+ "http://0.0.0.0:1420" ,
824+ "http://[::1]:1420" ,
825+ "http://[::]:1420" ,
826+ ] {
827+ assert ! ( is_localhost_url( & url. parse( ) . unwrap( ) ) , "{url}" ) ;
828+ }
829+
830+ for url in [
831+ "http://192.168.0.10:1420" ,
832+ "http://example.com" ,
833+ "http://[fe80::1]:1420" ,
834+ ] {
835+ assert ! ( !is_localhost_url( & url. parse( ) . unwrap( ) ) , "{url}" ) ;
836+ }
837+ }
838+
839+ #[ test]
840+ fn replacing_dev_url_host_keeps_query_and_fragment ( ) {
841+ let mut url: url:: Url = "http://[::1]:5173/app/index.html?foo=bar#/route"
842+ . parse ( )
843+ . unwrap ( ) ;
844+ url
845+ . set_ip_host ( IpAddr :: V4 ( Ipv4Addr :: new ( 192 , 168 , 0 , 10 ) ) )
846+ . unwrap ( ) ;
847+ assert_eq ! (
848+ url. as_str( ) ,
849+ "http://192.168.0.10:5173/app/index.html?foo=bar#/route"
850+ ) ;
851+ }
852+
817853 #[ test]
818854 fn detects_secret_env_vars ( ) {
819855 for name in [
0 commit comments