Skip to content

Commit 4d1411e

Browse files
committed
fix(core): loading remote URLs on mobile, closes #13461
1 parent 916aeaa commit 4d1411e

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

.changes/mobile-external-urls.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:bug
3+
---
4+
5+
Fixes loading external URLs in mobile development mode.

crates/tauri/src/manager/webview.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,11 @@ impl<R: Runtime> WebviewManager<R> {
413413
#[allow(unused_mut)] // mut url only for the data-url parsing
414414
let mut url = match &pending.webview_attributes.url {
415415
WebviewUrl::App(path) => {
416-
let url = if PROXY_DEV_SERVER {
416+
let app_url = app_manager.get_url(pending.webview_attributes.use_https_scheme);
417+
let url = if PROXY_DEV_SERVER && is_local_network_url(&app_url) {
417418
Cow::Owned(Url::parse("tauri://localhost").unwrap())
418419
} else {
419-
app_manager.get_url(pending.webview_attributes.use_https_scheme)
420+
app_url
420421
};
421422
// ignore "index.html" just to simplify the url
422423
if path.to_str() != Some("index.html") {
@@ -431,13 +432,13 @@ impl<R: Runtime> WebviewManager<R> {
431432
}
432433
WebviewUrl::External(url) => {
433434
let config_url = app_manager.get_url(pending.webview_attributes.use_https_scheme);
434-
let is_local = config_url.make_relative(url).is_some();
435+
let is_app_url = config_url.make_relative(url).is_some();
435436
let mut url = url.clone();
436-
if is_local && PROXY_DEV_SERVER {
437-
url.set_scheme("tauri").unwrap();
438-
url.set_host(Some("localhost")).unwrap();
437+
if is_app_url && PROXY_DEV_SERVER && is_local_network_url(&url) {
438+
Url::parse("tauri://localhost").unwrap()
439+
} else {
440+
url
439441
}
440-
url
441442
}
442443

443444
WebviewUrl::CustomProtocol(url) => url.clone(),
@@ -710,3 +711,29 @@ fn on_webview_event<R: Runtime>(webview: &Webview<R>, event: &WebviewEvent) -> c
710711

711712
Ok(())
712713
}
714+
715+
fn is_local_network_url(url: &url::Url) -> bool {
716+
match url.host() {
717+
Some(url::Host::Domain(s)) => s == "localhost",
718+
Some(url::Host::Ipv4(_)) | Some(url::Host::Ipv6(_)) => true,
719+
None => false,
720+
}
721+
}
722+
723+
#[cfg(test)]
724+
mod tests {
725+
use super::*;
726+
727+
#[test]
728+
fn local_network_url() {
729+
assert!(is_local_network_url(&"http://localhost".parse().unwrap()));
730+
assert!(is_local_network_url(
731+
&"http://127.0.0.1:8080".parse().unwrap()
732+
));
733+
assert!(is_local_network_url(
734+
&"https://192.168.3.17".parse().unwrap()
735+
));
736+
737+
assert!(!is_local_network_url(&"https://tauri.app".parse().unwrap()));
738+
}
739+
}

0 commit comments

Comments
 (0)