Skip to content

Commit 78eaada

Browse files
refactor(core): only proxy on mobile (#6126)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent 8cc1114 commit 78eaada

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

.changes/only-proxy-on-mobile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Only proxy the dev server on mobile to simplify desktop usage.

core/tauri/src/manager.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ const WINDOW_FILE_DROP_HOVER_EVENT: &str = "tauri://file-drop-hover";
7373
const WINDOW_FILE_DROP_CANCELLED_EVENT: &str = "tauri://file-drop-cancelled";
7474
const MENU_EVENT: &str = "tauri://menu";
7575

76+
// we need to proxy the dev server on mobile because we can't use `localhost`, so we use the local IP address
77+
// and we do not get a secure context without the custom protocol that proxies to the dev server
78+
// additionally, we need the custom protocol to inject the initialization scripts on Android
79+
// must also keep in sync with the `let mut response` assignment in prepare_uri_scheme_protocol
80+
const PROXY_DEV_SERVER: bool = cfg!(all(dev, mobile));
81+
7682
#[derive(Default)]
7783
/// Spaced and quoted Content-Security-Policy hash values.
7884
struct CspHashStrings {
@@ -373,7 +379,7 @@ impl<R: Runtime> WindowManager<R> {
373379
fn get_browser_origin(&self) -> String {
374380
match self.base_path() {
375381
AppUrl::Url(WindowUrl::External(url)) => {
376-
if cfg!(dev) && !cfg!(target_os = "linux") {
382+
if PROXY_DEV_SERVER {
377383
format_real_schema("tauri")
378384
} else {
379385
url.origin().ascii_serialization()
@@ -884,36 +890,37 @@ impl<R: Runtime> WindowManager<R> {
884890
>,
885891
) -> Box<dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync>
886892
{
887-
#[cfg(dev)]
893+
#[cfg(all(dev, mobile))]
888894
let url = {
889895
let mut url = self.get_url().as_str().to_string();
890896
if url.ends_with('/') {
891897
url.pop();
892898
}
893899
url
894900
};
895-
#[cfg(not(dev))]
901+
#[cfg(not(all(dev, mobile)))]
896902
let manager = self.clone();
897903
let window_origin = window_origin.to_string();
898904

899-
#[cfg(dev)]
905+
#[cfg(all(dev, mobile))]
900906
#[derive(Clone)]
901907
struct CachedResponse {
902908
status: http::StatusCode,
903909
headers: http::HeaderMap,
904910
body: Cow<'static, [u8]>,
905911
}
906912

907-
#[cfg(dev)]
913+
#[cfg(all(dev, mobile))]
908914
let response_cache = Arc::new(Mutex::new(HashMap::new()));
909915

910916
Box::new(move |request| {
911917
// use the entire URI as we are going to proxy the request
912-
#[cfg(dev)]
913-
let path = request.uri();
914-
// ignore query string and fragment
915-
#[cfg(not(dev))]
916-
let path = request.uri().split(&['?', '#'][..]).next().unwrap();
918+
let path = if PROXY_DEV_SERVER {
919+
request.uri()
920+
} else {
921+
// ignore query string and fragment
922+
request.uri().split(&['?', '#'][..]).next().unwrap()
923+
};
917924

918925
let path = path
919926
.strip_prefix("tauri://localhost")
@@ -925,7 +932,7 @@ impl<R: Runtime> WindowManager<R> {
925932
let mut builder =
926933
HttpResponseBuilder::new().header("Access-Control-Allow-Origin", &window_origin);
927934

928-
#[cfg(dev)]
935+
#[cfg(all(dev, mobile))]
929936
let mut response = {
930937
use attohttpc::StatusCode;
931938
let decoded_path = percent_encoding::percent_decode(path.as_bytes())
@@ -970,7 +977,7 @@ impl<R: Runtime> WindowManager<R> {
970977
}
971978
};
972979

973-
#[cfg(not(dev))]
980+
#[cfg(not(all(dev, mobile)))]
974981
let mut response = {
975982
let asset = manager.get_asset(path)?;
976983
builder = builder.mimetype(&asset.mime_type);
@@ -1207,10 +1214,11 @@ impl<R: Runtime> WindowManager<R> {
12071214
#[allow(unused_mut)] // mut url only for the data-url parsing
12081215
let (is_local, mut url) = match &pending.webview_attributes.url {
12091216
WindowUrl::App(path) => {
1210-
#[cfg(target_os = "linux")]
1211-
let url = self.get_url();
1212-
#[cfg(not(target_os = "linux"))]
1213-
let url: Cow<'_, Url> = Cow::Owned(Url::parse("tauri://localhost").unwrap());
1217+
let url = if PROXY_DEV_SERVER {
1218+
Cow::Owned(Url::parse("tauri://localhost").unwrap())
1219+
} else {
1220+
self.get_url()
1221+
};
12141222
(
12151223
true,
12161224
// ignore "index.html" just to simplify the url
@@ -1229,7 +1237,7 @@ impl<R: Runtime> WindowManager<R> {
12291237
let config_url = self.get_url();
12301238
let is_local = config_url.make_relative(url).is_some();
12311239
let mut url = url.clone();
1232-
if is_local && !cfg!(target_os = "linux") {
1240+
if is_local && PROXY_DEV_SERVER {
12331241
url.set_scheme("tauri").unwrap();
12341242
url.set_host(Some("localhost")).unwrap();
12351243
}

0 commit comments

Comments
 (0)