mirror of
https://github.com/leptos-rs/leptos
synced 2024-09-20 06:21:57 +00:00
fix: correctly handle external origins in redirect hook (closes #2269)
This commit is contained in:
parent
4222c832b1
commit
6766082536
1 changed files with 18 additions and 6 deletions
|
@ -58,13 +58,25 @@ pub fn Router(
|
|||
let navigate = SendWrapper::new(navigate);
|
||||
let router_hook = Box::new(move |path: &str| {
|
||||
let path = path.to_string();
|
||||
let Ok(origin) = window().location().origin() else {
|
||||
return;
|
||||
};
|
||||
let Ok(to_url) = web_sys::Url::new_with_base(&path, &origin) else {
|
||||
logging::error!("could not parse url {path:?}");
|
||||
return;
|
||||
};
|
||||
let to_origin = to_url.origin();
|
||||
// delay by a tick here, so that the Action updates *before* the redirect
|
||||
request_animation_frame({
|
||||
let navigate = navigate.clone();
|
||||
move || {
|
||||
navigate(&path, Default::default());
|
||||
}
|
||||
});
|
||||
if to_origin != origin {
|
||||
_ = window().location().set_href(&path);
|
||||
} else {
|
||||
request_animation_frame({
|
||||
let navigate = navigate.clone();
|
||||
move || {
|
||||
navigate(&path, Default::default());
|
||||
}
|
||||
});
|
||||
}
|
||||
}) as RedirectHook;
|
||||
_ = server_fn::redirect::set_redirect_hook(router_hook);
|
||||
|
||||
|
|
Loading…
Reference in a new issue