Skip to content

Commit

Permalink
fix(cli): use local ip addr for built-in server on mobile, closes #6454
Browse files Browse the repository at this point in the history
… (#6631)

Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
fix(cli): use local ip addr for built-in server on mobile, closes #6454
  • Loading branch information
amrbashir and lucasfernog committed Apr 4, 2023
1 parent 29ee623 commit 7fec0f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-built-in-dev-server-mobile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'cli.rs': 'patch'
'cli.js': 'patch'
---

Use local ip address for built-in dev server on mobile.
7 changes: 6 additions & 1 deletion tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,12 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
use crate::helpers::web_dev_server::start_dev_server;
if path.exists() {
let path = path.canonicalize()?;
let server_url = start_dev_server(path, options.port)?;
let ip = if mobile {
*local_ip_address(options.force_ip_prompt)
} else {
Ipv4Addr::new(127, 0, 0, 1).into()
};
let server_url = start_dev_server(path, ip, options.port)?;
let server_url = format!("http://{server_url}");
dev_path = AppUrl::Url(WindowUrl::External(server_url.parse().unwrap()));

Expand Down
10 changes: 7 additions & 3 deletions tooling/cli/src/helpers/web_dev_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use kuchiki::{traits::TendrilSink, NodeRef};
use notify::RecursiveMode;
use notify_debouncer_mini::new_debouncer;
use std::{
net::{Ipv4Addr, SocketAddr},
net::{IpAddr, SocketAddr},
path::{Path, PathBuf},
sync::{mpsc::sync_channel, Arc},
thread,
Expand All @@ -31,7 +31,11 @@ struct State {
tx: Sender<()>,
}

pub fn start_dev_server<P: AsRef<Path>>(path: P, port: Option<u16>) -> crate::Result<SocketAddr> {
pub fn start_dev_server<P: AsRef<Path>>(
path: P,
ip: IpAddr,
port: Option<u16>,
) -> crate::Result<SocketAddr> {
let serve_dir = path.as_ref().to_path_buf();

let (server_url_tx, server_url_rx) = std::sync::mpsc::channel();
Expand Down Expand Up @@ -79,7 +83,7 @@ pub fn start_dev_server<P: AsRef<Path>>(path: P, port: Option<u16>) -> crate::Re
});

let (server, server_url) = loop {
let server_url = SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), port);
let server_url = SocketAddr::new(ip, port);
let server = Server::try_bind(&server_url);

if !auto_port {
Expand Down

0 comments on commit 7fec0f0

Please sign in to comment.