Skip to content

Commit 22dde1e

Browse files
nathan-thillairajahDylan Gclaude
committed
fix(browser): return to the app's live route after a handoff
A hosted app that changes its route with pushState reports no page load, so the shared snapshot kept the earlier URL and coming back from the system browser reloaded that route. The handoff now reads the guest's own URL, falling back to the snapshot. Co-authored-by: Dylan G <69953f73c9f5149d56f2155107338a5435823b938a6c430c63e779f9a8a446f9@remote-agent-pilot.communities.buzz.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Nathan Thillairajah <[email protected]>
1 parent 993b587 commit 22dde1e

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎src-tauri/src/browser/mod.rs‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod platform;
55
mod policy;
66

77
use std::{
8-
cell::{Cell, RefCell},
8+
cell::{Cell, OnceCell, RefCell},
99
rc::Rc,
1010
sync::atomic::{AtomicU64, Ordering},
1111
};
@@ -314,13 +314,21 @@ struct Handoff {
314314
open: Box<dyn Fn(&str) -> bool>,
315315
approvals: Rc<RefCell<Approvals>>,
316316
snapshot: Rc<RefCell<BrowserSnapshot>>,
317+
// The guest's own URL, set once the guest exists. The shared snapshot
318+
// misses a page's own history changes, such as pushState.
319+
location: OnceCell<Box<dyn Fn() -> Option<String>>>,
317320
}
318321

319322
impl Handoff {
320323
/// The page the guest shows, when it is on `app`: only the app being
321324
/// opened hands off its own portal pages.
322325
fn shown(&self, app: &str) -> Option<Url> {
323-
let current = Url::parse(&self.snapshot.borrow().url).ok()?;
326+
let current = self
327+
.location
328+
.get()
329+
.and_then(|location| location())
330+
.unwrap_or_else(|| self.snapshot.borrow().url.clone());
331+
let current = Url::parse(&current).ok()?;
324332
(current.host_str() == Url::parse(app).ok()?.host_str()).then_some(current)
325333
}
326334

@@ -448,6 +456,7 @@ fn build_contents(
448456
}),
449457
approvals: approvals.clone(),
450458
snapshot: snapshot.clone(),
459+
location: OnceCell::new(),
451460
});
452461
let navigation_handoff = handoff.clone();
453462
// No URL is loaded until native permission handlers replace Wry's defaults.
@@ -486,6 +495,7 @@ fn build_contents(
486495
})
487496
.with_new_window_req_handler(|_, _| wry::NewWindowResponse::Deny);
488497
let guest = platform::build_guest(builder, &host.window())?;
498+
let _ = handoff.location.set(platform::location(&guest));
489499
let permissions = platform::deny_permissions(&guest)?;
490500
let navigation = platform::observe_navigation(&guest)?;
491501
let browser = BrowserSession {
@@ -828,6 +838,7 @@ mod handoff_tests {
828838
loading: true,
829839
..BrowserSnapshot::default()
830840
})),
841+
location: OnceCell::new(),
831842
};
832843
(handoff, opened)
833844
}
@@ -879,6 +890,26 @@ mod handoff_tests {
879890
assert_eq!(opened.borrow().len(), 2);
880891
}
881892

893+
#[test]
894+
fn coming_back_returns_to_the_route_the_app_moved_to_itself() {
895+
let (handoff, opened) = handoff();
896+
// The app changed its route with pushState; no page load reported it.
897+
let _ = handoff.location.set(Box::new(|| {
898+
Some("https://a.apps.builderlab.xyz/reports/43".into())
899+
}));
900+
let github = format!("{CONNECT}?extension=github&app=a.apps.builderlab.xyz");
901+
assert!(handoff.intercept(&github));
902+
assert_eq!(*opened.borrow(), std::slice::from_ref(&github));
903+
assert_eq!(
904+
handoff
905+
.approvals
906+
.borrow_mut()
907+
.take_return()
908+
.map(|next| next.app),
909+
Some("https://a.apps.builderlab.xyz/reports/43".into())
910+
);
911+
}
912+
882913
#[test]
883914
fn hands_off_this_apps_connections_page_and_holds_a_later_one() {
884915
let (handoff, opened) = handoff();

‎src-tauri/src/browser/platform.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,26 @@ pub(super) fn set_media_suspended(_guest: &WebView, _suspended: bool) -> Result<
498498
Err("Suspending a page's media is supported only on macOS".into())
499499
}
500500

501+
/// Reads the guest's URL from its own handlers, which the guest owns, so
502+
/// this holds it weakly.
503+
#[cfg(target_os = "macos")]
504+
pub(super) fn location(guest: &WebView) -> Box<dyn Fn() -> Option<String>> {
505+
use wry::WebViewExtMacOS;
506+
let guest = objc2::rc::Weak::from_retained(&guest.webview());
507+
Box::new(move || {
508+
let guest = guest.load()?;
509+
unsafe { guest.URL() }
510+
.and_then(|url| url.absoluteString())
511+
.map(|url| url.to_string())
512+
})
513+
}
514+
515+
/// Elsewhere the page-load URL stands in.
516+
#[cfg(not(target_os = "macos"))]
517+
pub(super) fn location(_guest: &WebView) -> Box<dyn Fn() -> Option<String>> {
518+
Box::new(|| None)
519+
}
520+
501521
#[cfg(not(target_os = "macos"))]
502522
pub(super) fn current_url(guest: &WebView) -> Result<Option<String>, String> {
503523
guest.url().map(Some).map_err(|error| error.to_string())

0 commit comments

Comments
 (0)