mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +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 navigate = SendWrapper::new(navigate);
|
||||||
let router_hook = Box::new(move |path: &str| {
|
let router_hook = Box::new(move |path: &str| {
|
||||||
let path = path.to_string();
|
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
|
// delay by a tick here, so that the Action updates *before* the redirect
|
||||||
|
if to_origin != origin {
|
||||||
|
_ = window().location().set_href(&path);
|
||||||
|
} else {
|
||||||
request_animation_frame({
|
request_animation_frame({
|
||||||
let navigate = navigate.clone();
|
let navigate = navigate.clone();
|
||||||
move || {
|
move || {
|
||||||
navigate(&path, Default::default());
|
navigate(&path, Default::default());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}) as RedirectHook;
|
}) as RedirectHook;
|
||||||
_ = server_fn::redirect::set_redirect_hook(router_hook);
|
_ = server_fn::redirect::set_redirect_hook(router_hook);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue