From fca850ca73928d22b5ca2de64d5ebdc4e5be4fd6 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 1 Nov 2025 21:16:42 -0300 Subject: [PATCH] proxy dev server --- crates/tauri/Cargo.toml | 17 ++++++++--------- crates/tauri/src/manager/webview.rs | 2 +- crates/tauri/src/protocol/tauri.rs | 27 +++++++++------------------ examples/api/src-tauri/Cargo.toml | 1 + examples/api/src-tauri/src/lib.rs | 7 +++++-- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/crates/tauri/Cargo.toml b/crates/tauri/Cargo.toml index c2cb91695..9351cbf7e 100644 --- a/crates/tauri/Cargo.toml +++ b/crates/tauri/Cargo.toml @@ -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" diff --git a/crates/tauri/src/manager/webview.rs b/crates/tauri/src/manager/webview.rs index 623b31d24..4cdf62cef 100644 --- a/crates/tauri/src/manager/webview.rs +++ b/crates/tauri/src/manager/webview.rs @@ -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"); diff --git a/crates/tauri/src/protocol/tauri.rs b/crates/tauri/src/protocol/tauri.rs index 52ba6119d..199e35641 100644 --- a/crates/tauri/src/protocol/tauri.rs +++ b/crates/tauri/src/protocol/tauri.rs @@ -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( window_origin: &str, web_resource_request_handler: Option>, ) -> 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( 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( &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( #[allow(unused_variables)] manager: &AppManager, window_origin: &str, web_resource_request_handler: Option<&WebResourceRequestHandler>, - #[cfg(all(dev, mobile))] (url, response_cache): ( - &str, - &Arc>>, - ), + (url, response_cache): (&str, &Arc>>), ) -> Result>, Box> { // 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( .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( 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( } builder.body(asset.bytes.into())? }; + if let Some(handler) = &web_resource_request_handler { handler(request, &mut response); } diff --git a/examples/api/src-tauri/Cargo.toml b/examples/api/src-tauri/Cargo.toml index 2e6243190..9982261a8 100644 --- a/examples/api/src-tauri/Cargo.toml +++ b/examples/api/src-tauri/Cargo.toml @@ -41,3 +41,4 @@ features = ["test"] [features] prod = ["tauri/custom-protocol"] +cef = ["tauri/cef"] diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index 73a573672..6952d5043 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -31,7 +31,10 @@ pub struct PopupMenu(tauri::menu::Menu); #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { - run_app(tauri::Builder::default(), |_app| {}) + #[cfg(feature = "cef")] + run_app(tauri::Builder::::default(), |_app| {}); + #[cfg(not(feature = "cef"))] + run_app(tauri::Builder::default(), |_app| {}); } pub fn run_app) + Send + 'static>( @@ -47,7 +50,7 @@ pub fn run_app) + 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)?;