diff --git a/.changes/core-navigation-handler.md b/.changes/core-navigation-handler.md new file mode 100644 index 000000000..ea91d5e04 --- /dev/null +++ b/.changes/core-navigation-handler.md @@ -0,0 +1,5 @@ +--- +'tauri': 'patch' +--- + +Fix `WindowBuilder::on_navigation` handler not registered properly. diff --git a/core/tauri-runtime/src/window.rs b/core/tauri-runtime/src/window.rs index af15dac05..8364c36b1 100644 --- a/core/tauri-runtime/src/window.rs +++ b/core/tauri-runtime/src/window.rs @@ -24,6 +24,8 @@ use std::{ type UriSchemeProtocol = dyn Fn(&HttpRequest) -> Result> + Send + Sync + 'static; +type WebResourceRequestHandler = dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync; + /// UI scaling utilities. pub mod dpi; @@ -234,6 +236,8 @@ pub struct PendingWindow> { /// A handler to decide if incoming url is allowed to navigate. pub navigation_handler: Option bool + Send>>, + pub web_resource_request_handler: Option>, + /// The current webview URL. pub current_url: Arc>, } @@ -275,6 +279,7 @@ impl> PendingWindow { menu_ids: Arc::new(Mutex::new(menu_ids)), js_event_listeners: Default::default(), navigation_handler: Default::default(), + web_resource_request_handler: Default::default(), current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())), }) } @@ -305,6 +310,7 @@ impl> PendingWindow { menu_ids: Arc::new(Mutex::new(menu_ids)), js_event_listeners: Default::default(), navigation_handler: Default::default(), + web_resource_request_handler: Default::default(), current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())), }) } diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 544977324..f2db2494c 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -1705,10 +1705,9 @@ impl Builder { .collect::>(); for pending in self.pending_windows { - let pending = - app - .manager - .prepare_window(app.handle.clone(), pending, &window_labels, None)?; + let pending = app + .manager + .prepare_window(app.handle.clone(), pending, &window_labels)?; let detached = app.runtime.as_ref().unwrap().create_window(pending)?; let _window = app.manager.attach_window(app.handle(), detached); } diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index d613251ac..b7d8a2539 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -25,6 +25,7 @@ use tauri_utils::{ html::{SCRIPT_NONCE_TOKEN, STYLE_NONCE_TOKEN}, }; +use crate::app::{GlobalMenuEventListener, WindowMenuEvent}; use crate::hooks::IpcJavascript; #[cfg(feature = "isolation")] use crate::hooks::IsolationJavascript; @@ -50,10 +51,6 @@ use crate::{ Context, EventLoopMessage, Icon, Invoke, Manager, Pattern, Runtime, Scopes, StateManager, Window, WindowEvent, }; -use crate::{ - app::{GlobalMenuEventListener, WindowMenuEvent}, - window::WebResourceRequestHandler, -}; #[cfg(any(target_os = "linux", target_os = "windows"))] use crate::api::path::{resolve_path, BaseDirectory}; @@ -401,7 +398,6 @@ impl WindowManager { label: &str, window_labels: &[String], app_handle: AppHandle, - web_resource_request_handler: Option>, ) -> crate::Result> { let is_init_global = self.inner.config.build.with_global_tauri; let plugin_init = self @@ -492,6 +488,7 @@ impl WindowManager { }; if !registered_scheme_protocols.contains(&"tauri".into()) { + let web_resource_request_handler = pending.web_resource_request_handler.take(); pending.register_uri_scheme_protocol( "tauri", self.prepare_uri_scheme_protocol(&window_origin, web_resource_request_handler), @@ -1122,7 +1119,6 @@ impl WindowManager { app_handle: AppHandle, mut pending: PendingWindow, window_labels: &[String], - web_resource_request_handler: Option>, ) -> crate::Result> { if self.windows_lock().contains_key(&pending.label) { return Err(crate::Error::WindowLabelAlreadyExists(pending.label)); @@ -1186,13 +1182,7 @@ impl WindowManager { } let label = pending.label.clone(); - pending = self.prepare_pending_window( - pending, - &label, - window_labels, - app_handle.clone(), - web_resource_request_handler, - )?; + pending = self.prepare_pending_window(pending, &label, window_labels, app_handle.clone())?; pending.ipc_handler = Some(self.prepare_ipc_handler(app_handle)); // in `Windows`, we need to force a data_directory diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index b14b6d2cb..e8b4d11fa 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -310,19 +310,18 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> { /// Creates a new webview window. pub fn build(mut self) -> crate::Result> { - let web_resource_request_handler = self.web_resource_request_handler.take(); - let pending = PendingWindow::new( + let mut pending = PendingWindow::new( self.window_builder.clone(), self.webview_attributes.clone(), self.label.clone(), )?; + pending.navigation_handler = self.navigation_handler.take(); + pending.web_resource_request_handler = self.web_resource_request_handler.take(); + let labels = self.manager.labels().into_iter().collect::>(); - let pending = self.manager.prepare_window( - self.app_handle.clone(), - pending, - &labels, - web_resource_request_handler, - )?; + let pending = self + .manager + .prepare_window(self.app_handle.clone(), pending, &labels)?; let window = match &mut self.runtime { RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),