Skip to content

Commit 8e03c00

Browse files
committed
avoid panic in use_navigate during SSR
1 parent 8096524 commit 8e03c00

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

router/src/hooks.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ pub fn use_resolved_path(
170170

171171
/// Returns a function that can be used to navigate to a new route.
172172
///
173+
/// This should only be called on the client; it does nothing during
174+
/// server rendering.
175+
///
173176
/// ```rust
174177
/// # use leptos::{request_animation_frame, create_runtime};
175178
/// # let runtime = create_runtime();
@@ -185,11 +188,18 @@ pub fn use_navigate() -> impl Fn(&str, NavigateOptions) {
185188
move |to, options| {
186189
let router = Rc::clone(&router.inner);
187190
let to = to.to_string();
188-
request_animation_frame(move || {
189-
if let Err(e) = router.navigate_from_route(&to, &options) {
190-
leptos::debug_warn!("use_navigate error: {e:?}");
191-
}
192-
});
191+
if cfg!(any(feature = "csr", feature = "hydrate")) {
192+
request_animation_frame(move || {
193+
if let Err(e) = router.navigate_from_route(&to, &options) {
194+
leptos::debug_warn!("use_navigate error: {e:?}");
195+
}
196+
});
197+
} else {
198+
leptos::warn!(
199+
"The navigation function returned by `use_navigate` should \
200+
not be called during server rendering."
201+
);
202+
}
193203
}
194204
}
195205

0 commit comments

Comments
 (0)