proxy dev server

This commit is contained in:
Lucas Nogueira
2025-11-01 21:16:42 -03:00
parent 8c3539ab99
commit fca850ca73
5 changed files with 24 additions and 30 deletions

View File

@@ -94,6 +94,14 @@ tray-icon = { version = "0.21", default-features = false, features = [
"serde",
], optional = true }
# dev server proxy
bytes = { version = "1", features = ["serde"] }
reqwest = { version = "0.12", default-features = false, features = [
"json",
"stream",
] }
# linux
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies]
gtk = { version = "0.18", features = ["v3_24"] }
@@ -138,15 +146,6 @@ windows = { version = "0.61", features = [
"Win32_UI_WindowsAndMessaging",
] }
# mobile
[target.'cfg(any(target_os = "android", all(target_vendor = "apple", not(target_os = "macos"))))'.dependencies]
bytes = { version = "1", features = ["serde"] }
reqwest = { version = "0.12", default-features = false, features = [
"json",
"stream",
] }
# android
[target.'cfg(target_os = "android")'.dependencies]
jni = "0.21"

View File

@@ -37,7 +37,7 @@ use super::{
// and we do not get a secure context without the custom protocol that proxies to the dev server
// additionally, we need the custom protocol to inject the initialization scripts on Android
// must also keep in sync with the `let mut response` assignment in prepare_uri_scheme_protocol
pub(crate) const PROXY_DEV_SERVER: bool = cfg!(all(dev, mobile));
pub(crate) const PROXY_DEV_SERVER: bool = cfg!(all(dev, any(mobile, feature = "cef")));
pub(crate) const PROCESS_IPC_MESSAGE_FN: &str =
include_str!("../../scripts/process-ipc-message-fn.js");

View File

@@ -2,7 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{borrow::Cow, sync::Arc};
use std::{
borrow::Cow,
collections::HashMap,
sync::{Arc, Mutex},
};
use http::{header::CONTENT_TYPE, Request, Response as HttpResponse, StatusCode};
use tauri_utils::config::HeaderAddition;
@@ -13,10 +17,6 @@ use crate::{
Runtime,
};
#[cfg(all(dev, mobile))]
use std::{collections::HashMap, sync::Mutex};
#[cfg(all(dev, mobile))]
#[derive(Clone)]
struct CachedResponse {
status: http::StatusCode,
@@ -29,7 +29,6 @@ pub fn get<R: Runtime>(
window_origin: &str,
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
) -> UriSchemeProtocolHandler {
#[cfg(all(dev, mobile))]
let url = {
let mut url = manager
.get_url(window_origin.starts_with("https"))
@@ -43,7 +42,6 @@ pub fn get<R: Runtime>(
let window_origin = window_origin.to_string();
#[cfg(all(dev, mobile))]
let response_cache = Arc::new(Mutex::new(HashMap::new()));
Box::new(move |_, request, responder| {
@@ -52,7 +50,6 @@ pub fn get<R: Runtime>(
&manager,
&window_origin,
web_resource_request_handler.as_deref(),
#[cfg(all(dev, mobile))]
(&url, &response_cache),
) {
Ok(response) => responder.respond(response),
@@ -73,10 +70,7 @@ fn get_response<R: Runtime>(
#[allow(unused_variables)] manager: &AppManager<R>,
window_origin: &str,
web_resource_request_handler: Option<&WebResourceRequestHandler>,
#[cfg(all(dev, mobile))] (url, response_cache): (
&str,
&Arc<Mutex<HashMap<String, CachedResponse>>>,
),
(url, response_cache): (&str, &Arc<Mutex<HashMap<String, CachedResponse>>>),
) -> Result<HttpResponse<Cow<'static, [u8]>>, Box<dyn std::error::Error>> {
// use the entire URI as we are going to proxy the request
let path = if PROXY_DEV_SERVER {
@@ -103,8 +97,7 @@ fn get_response<R: Runtime>(
.add_configured_headers(manager.config.app.security.headers.as_ref())
.header("Access-Control-Allow-Origin", window_origin);
#[cfg(all(dev, mobile))]
let mut response = {
let mut response = if PROXY_DEV_SERVER && manager.assets.iter().count() == 0 {
let decoded_path = percent_encoding::percent_decode(path.as_bytes())
.decode_utf8_lossy()
.to_string();
@@ -203,10 +196,7 @@ fn get_response<R: Runtime>(
return Err(error_message.into());
}
}
};
#[cfg(not(all(dev, mobile)))]
let mut response = {
} else {
let use_https_scheme = request.uri().scheme() == Some(&http::uri::Scheme::HTTPS);
let asset = manager.get_asset(path, use_https_scheme)?;
builder = builder.header(CONTENT_TYPE, &asset.mime_type);
@@ -215,6 +205,7 @@ fn get_response<R: Runtime>(
}
builder.body(asset.bytes.into())?
};
if let Some(handler) = &web_resource_request_handler {
handler(request, &mut response);
}

View File

@@ -41,3 +41,4 @@ features = ["test"]
[features]
prod = ["tauri/custom-protocol"]
cef = ["tauri/cef"]

View File

@@ -31,7 +31,10 @@ pub struct PopupMenu<R: Runtime>(tauri::menu::Menu<R>);
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
run_app(tauri::Builder::default(), |_app| {})
#[cfg(feature = "cef")]
run_app(tauri::Builder::<tauri::Cef>::default(), |_app| {});
#[cfg(not(feature = "cef"))]
run_app(tauri::Builder::default(), |_app| {});
}
pub fn run_app<R: Runtime, F: FnOnce(&App<R>) + Send + 'static>(
@@ -47,7 +50,7 @@ pub fn run_app<R: Runtime, F: FnOnce(&App<R>) + Send + 'static>(
)
.plugin(tauri_plugin_sample::init())
.setup(move |app| {
#[cfg(all(desktop, not(test)))]
#[cfg(all(desktop, not(test), not(feature = "cef")))]
{
let handle = app.handle();
tray::create_tray(handle)?;