From 800eade58d1c09d19f283d0a014b9e00bef9cc81 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Fri, 29 May 2026 09:32:39 +0800 Subject: [PATCH] perf(mobile): reuse dev reqwest client (#15444) --- .changes/reuse-mobile-dev-reqwest-client.md | 5 + crates/tauri/src/protocol/tauri.rs | 107 ++++++++++---------- 2 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 .changes/reuse-mobile-dev-reqwest-client.md diff --git a/.changes/reuse-mobile-dev-reqwest-client.md b/.changes/reuse-mobile-dev-reqwest-client.md new file mode 100644 index 000000000000..fcfe0125cff1 --- /dev/null +++ b/.changes/reuse-mobile-dev-reqwest-client.md @@ -0,0 +1,5 @@ +--- +tauri: patch:perf +--- + +Reuse proxy reqwest client in mobile dev, improving the dev load speed diff --git a/crates/tauri/src/protocol/tauri.rs b/crates/tauri/src/protocol/tauri.rs index a346754f09d5..59343b58c912 100644 --- a/crates/tauri/src/protocol/tauri.rs +++ b/crates/tauri/src/protocol/tauri.rs @@ -29,22 +29,59 @@ pub fn get( window_origin: &str, web_resource_request_handler: Option>, ) -> UriSchemeProtocolHandler { + let window_origin = window_origin.to_string(); + #[cfg(all(dev, mobile))] - let url = { - let mut url = manager - .get_app_url(window_origin.starts_with("https")) - .as_str() - .to_string(); + let (url, client, response_cache) = { + let use_https = window_origin.starts_with("https"); + let mut url = manager.get_app_url(use_https).as_str().to_string(); if url.ends_with('/') { url.pop(); } - url - }; - let window_origin = window_origin.to_string(); + let mut client_builder = reqwest::ClientBuilder::new(); + if use_https { + #[cfg(feature = "rustls-tls")] + if rustls::crypto::CryptoProvider::get_default().is_none() { + let _ = rustls::crypto::ring::default_provider().install_default(); + } - #[cfg(all(dev, mobile))] - let response_cache = Arc::new(Mutex::new(HashMap::new())); + // we can't load env vars at runtime, gotta embed them in the lib + if let Some(cert_pem) = option_env!("TAURI_DEV_ROOT_CERTIFICATE") { + #[cfg(any( + feature = "native-tls", + feature = "native-tls-vendored", + feature = "rustls-tls" + ))] + { + log::info!("adding dev server root certificate"); + let certificate = reqwest::Certificate::from_pem(cert_pem.as_bytes()) + .expect("failed to parse TAURI_DEV_ROOT_CERTIFICATE"); + client_builder = client_builder.tls_certs_merge([certificate]); + } + + #[cfg(not(any( + feature = "native-tls", + feature = "native-tls-vendored", + feature = "rustls-tls" + )))] + { + log::warn!( + "the dev root-certificate-path option was provided, but you must enable one of the following Tauri features in Cargo.toml: native-tls, native-tls-vendored, rustls-tls" + ); + } + } else { + log::warn!( + "loading HTTPS URL; you might need to provide a certificate via the `dev --root-certificate-path` option. You must enable one of the following Tauri features in Cargo.toml: native-tls, native-tls-vendored, rustls-tls" + ); + } + } + let client = client_builder.build().unwrap(); + + let response_cache = Arc::new(Mutex::new(HashMap::new())); + + (url, client, response_cache) + }; Box::new(move |_, request, responder| { match get_response( @@ -53,7 +90,7 @@ pub fn get( &window_origin, web_resource_request_handler.as_deref(), #[cfg(all(dev, mobile))] - (&url, &response_cache), + (&url, &client, &response_cache), ) { Ok(response) => responder.respond(response), Err(e) => responder.respond( @@ -73,8 +110,9 @@ fn get_response( #[allow(unused_variables)] manager: &AppManager, window_origin: &str, web_resource_request_handler: Option<&WebResourceRequestHandler>, - #[cfg(all(dev, mobile))] (url, response_cache): ( + #[cfg(all(dev, mobile))] (url, client, response_cache): ( &str, + &reqwest::Client, &Arc>>, ), ) -> Result>, Box> { @@ -114,50 +152,7 @@ fn get_response( decoded_path.trim_start_matches('/') ); - #[cfg(feature = "rustls-tls")] - if rustls::crypto::CryptoProvider::get_default().is_none() { - let _ = rustls::crypto::ring::default_provider().install_default(); - } - - let mut client = reqwest::ClientBuilder::new(); - - if url.starts_with("https://") { - // we can't load env vars at runtime, gotta embed them in the lib - if let Some(cert_pem) = option_env!("TAURI_DEV_ROOT_CERTIFICATE") { - #[cfg(any( - feature = "native-tls", - feature = "native-tls-vendored", - feature = "rustls-tls" - ))] - { - log::info!("adding dev server root certificate"); - let certificate = reqwest::Certificate::from_pem(cert_pem.as_bytes()) - .expect("failed to parse TAURI_DEV_ROOT_CERTIFICATE"); - client = client.tls_certs_merge([certificate]); - } - - #[cfg(not(any( - feature = "native-tls", - feature = "native-tls-vendored", - feature = "rustls-tls" - )))] - { - log::warn!( - "the dev root-certificate-path option was provided, but you must enable one of the following Tauri features in Cargo.toml: native-tls, native-tls-vendored, rustls-tls" - ); - } - } else { - log::warn!( - "loading HTTPS URL; you might need to provide a certificate via the `dev --root-certificate-path` option. You must enable one of the following Tauri features in Cargo.toml: native-tls, native-tls-vendored, rustls-tls" - ); - } - } - - let mut proxy_builder = client - .build() - .unwrap() - .request(request.method().clone(), &url); - proxy_builder = proxy_builder.body(std::mem::take(request.body_mut())); + let mut proxy_builder = client.request(request.method().clone(), &url); for (name, value) in request.headers() { proxy_builder = proxy_builder.header(name, value); }