How to set the window url in rust at runtime? #5377
-
Hi All, Even if I'm struggling to wrap my head around rust, I've been enjoying tauri. My current simple use case is that I start a server (via sidecar) on some random port and I would like the WebView to connect to it once I know where that server is running. I've started from the splash screen example to get 2 windows while being able to switch between them. My current code is below and seems to just be missing just that #![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::api::process::{Command, CommandEvent};
use tauri::Manager;
fn main() {
tauri::Builder::default()
.setup(|app| {
let splashscreen_window = app.get_window("splashscreen").unwrap();
let main_window = app.get_window("main").unwrap();
let (mut rx, _) = Command::new_sidecar("server")
.expect("failed to create sidecar")
.spawn()
.expect("Failed to spawn server");
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
if line.contains("Network: ") {
let tokens: Vec<&str> = line.split("Network: ").collect();
println!("{}", tokens[1]);
splashscreen_window.close().unwrap();
main_window.show().unwrap(); // .set_url(tokens[1])
}
}
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running application");
} |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 4 replies
-
There indeed is no direct api for that (yet), so instead you need to use main_window.eval(&format!("window.location.replace('http://localhost:{}')", "1234")); |
Beta Was this translation helpful? Give feedback.
-
Awesome, that's working thanks! |
Beta Was this translation helpful? Give feedback.
-
Seems that after changing the location you lose access to the Tauri Command invoker from window.__TAURI__.tauri (Use case: I want to load my web application from my own remote web server, but still be able to invoke Tauri Commands.) |
Beta Was this translation helpful? Give feedback.
-
Tauri Commands are only available when content is served by tauri itself. |
Beta Was this translation helpful? Give feedback.
-
Thats too bad, was hoping to ditch Electron this time around. Thanks for the quick reply though, much appreciated. |
Beta Was this translation helpful? Give feedback.
-
But it should be possible to load a page into an iframe which can then communicate with the containing page which in turn should have access to Tauri commands and act as a bridge. Does this work? |
Beta Was this translation helpful? Give feedback.
There indeed is no direct api for that (yet), so instead you need to use
eval
to execute js code. Something like this: