Skip to content

Commit 800eade

Browse files
perf(mobile): reuse dev reqwest client (#15444)
1 parent c2b8f47 commit 800eade

2 files changed

Lines changed: 56 additions & 56 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri: patch:perf
3+
---
4+
5+
Reuse proxy reqwest client in mobile dev, improving the dev load speed

crates/tauri/src/protocol/tauri.rs

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,59 @@ pub fn get<R: Runtime>(
2929
window_origin: &str,
3030
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
3131
) -> UriSchemeProtocolHandler {
32+
let window_origin = window_origin.to_string();
33+
3234
#[cfg(all(dev, mobile))]
33-
let url = {
34-
let mut url = manager
35-
.get_app_url(window_origin.starts_with("https"))
36-
.as_str()
37-
.to_string();
35+
let (url, client, response_cache) = {
36+
let use_https = window_origin.starts_with("https");
37+
let mut url = manager.get_app_url(use_https).as_str().to_string();
3838
if url.ends_with('/') {
3939
url.pop();
4040
}
41-
url
42-
};
4341

44-
let window_origin = window_origin.to_string();
42+
let mut client_builder = reqwest::ClientBuilder::new();
43+
if use_https {
44+
#[cfg(feature = "rustls-tls")]
45+
if rustls::crypto::CryptoProvider::get_default().is_none() {
46+
let _ = rustls::crypto::ring::default_provider().install_default();
47+
}
4548

46-
#[cfg(all(dev, mobile))]
47-
let response_cache = Arc::new(Mutex::new(HashMap::new()));
49+
// we can't load env vars at runtime, gotta embed them in the lib
50+
if let Some(cert_pem) = option_env!("TAURI_DEV_ROOT_CERTIFICATE") {
51+
#[cfg(any(
52+
feature = "native-tls",
53+
feature = "native-tls-vendored",
54+
feature = "rustls-tls"
55+
))]
56+
{
57+
log::info!("adding dev server root certificate");
58+
let certificate = reqwest::Certificate::from_pem(cert_pem.as_bytes())
59+
.expect("failed to parse TAURI_DEV_ROOT_CERTIFICATE");
60+
client_builder = client_builder.tls_certs_merge([certificate]);
61+
}
62+
63+
#[cfg(not(any(
64+
feature = "native-tls",
65+
feature = "native-tls-vendored",
66+
feature = "rustls-tls"
67+
)))]
68+
{
69+
log::warn!(
70+
"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"
71+
);
72+
}
73+
} else {
74+
log::warn!(
75+
"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"
76+
);
77+
}
78+
}
79+
let client = client_builder.build().unwrap();
80+
81+
let response_cache = Arc::new(Mutex::new(HashMap::new()));
82+
83+
(url, client, response_cache)
84+
};
4885

4986
Box::new(move |_, request, responder| {
5087
match get_response(
@@ -53,7 +90,7 @@ pub fn get<R: Runtime>(
5390
&window_origin,
5491
web_resource_request_handler.as_deref(),
5592
#[cfg(all(dev, mobile))]
56-
(&url, &response_cache),
93+
(&url, &client, &response_cache),
5794
) {
5895
Ok(response) => responder.respond(response),
5996
Err(e) => responder.respond(
@@ -73,8 +110,9 @@ fn get_response<R: Runtime>(
73110
#[allow(unused_variables)] manager: &AppManager<R>,
74111
window_origin: &str,
75112
web_resource_request_handler: Option<&WebResourceRequestHandler>,
76-
#[cfg(all(dev, mobile))] (url, response_cache): (
113+
#[cfg(all(dev, mobile))] (url, client, response_cache): (
77114
&str,
115+
&reqwest::Client,
78116
&Arc<Mutex<HashMap<String, CachedResponse>>>,
79117
),
80118
) -> Result<HttpResponse<Cow<'static, [u8]>>, Box<dyn std::error::Error>> {
@@ -114,50 +152,7 @@ fn get_response<R: Runtime>(
114152
decoded_path.trim_start_matches('/')
115153
);
116154

117-
#[cfg(feature = "rustls-tls")]
118-
if rustls::crypto::CryptoProvider::get_default().is_none() {
119-
let _ = rustls::crypto::ring::default_provider().install_default();
120-
}
121-
122-
let mut client = reqwest::ClientBuilder::new();
123-
124-
if url.starts_with("https://") {
125-
// we can't load env vars at runtime, gotta embed them in the lib
126-
if let Some(cert_pem) = option_env!("TAURI_DEV_ROOT_CERTIFICATE") {
127-
#[cfg(any(
128-
feature = "native-tls",
129-
feature = "native-tls-vendored",
130-
feature = "rustls-tls"
131-
))]
132-
{
133-
log::info!("adding dev server root certificate");
134-
let certificate = reqwest::Certificate::from_pem(cert_pem.as_bytes())
135-
.expect("failed to parse TAURI_DEV_ROOT_CERTIFICATE");
136-
client = client.tls_certs_merge([certificate]);
137-
}
138-
139-
#[cfg(not(any(
140-
feature = "native-tls",
141-
feature = "native-tls-vendored",
142-
feature = "rustls-tls"
143-
)))]
144-
{
145-
log::warn!(
146-
"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"
147-
);
148-
}
149-
} else {
150-
log::warn!(
151-
"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"
152-
);
153-
}
154-
}
155-
156-
let mut proxy_builder = client
157-
.build()
158-
.unwrap()
159-
.request(request.method().clone(), &url);
160-
proxy_builder = proxy_builder.body(std::mem::take(request.body_mut()));
155+
let mut proxy_builder = client.request(request.method().clone(), &url);
161156
for (name, value) in request.headers() {
162157
proxy_builder = proxy_builder.header(name, value);
163158
}

0 commit comments

Comments
 (0)