diff --git a/packages/router/src/history/web.rs b/packages/router/src/history/web.rs index f7bd59dfa..4a395356f 100644 --- a/packages/router/src/history/web.rs +++ b/packages/router/src/history/web.rs @@ -58,8 +58,6 @@ pub struct WebHistory { do_scroll_restoration: bool, history: History, listener_navigation: Option, - #[allow(dead_code)] - listener_scroll: Option, listener_animation_frame: Arc>>, prefix: Option, window: Window, @@ -97,27 +95,7 @@ impl WebHistory { where ::Err: std::fmt::Display, { - let w = window().expect("access to `window`"); - let h = w.history().expect("`window` has access to `history`"); - let document = w.document().expect("`window` has access to `document`"); - - let myself = Self::new_inner( - prefix, - do_scroll_restoration, - EventListener::new(&document, "scroll", { - let mut last_updated = 0.0; - move |evt| { - // the time stamp in milliseconds - let time_stamp = evt.time_stamp(); - // throttle the scroll event to 100ms - if (time_stamp - last_updated) < 100.0 { - return; - } - update_scroll::(&w, &h); - last_updated = time_stamp; - } - }), - ); + let myself = Self::new_inner(prefix, do_scroll_restoration); let current_route = myself.current_route(); let current_url = current_route.to_string(); @@ -168,32 +146,23 @@ impl WebHistory { myself } - fn new_inner( - prefix: Option, - do_scroll_restoration: bool, - event_listener: EventListener, - ) -> Self + fn new_inner(prefix: Option, do_scroll_restoration: bool) -> Self where ::Err: std::fmt::Display, { let window = window().expect("access to `window`"); let history = window.history().expect("`window` has access to `history`"); - let listener_scroll = match do_scroll_restoration { - true => { - history - .set_scroll_restoration(ScrollRestoration::Manual) - .expect("`history` can set scroll restoration"); - Some(event_listener) - } - false => None, - }; + if do_scroll_restoration { + history + .set_scroll_restoration(ScrollRestoration::Manual) + .expect("`history` can set scroll restoration"); + } Self { do_scroll_restoration, history, listener_navigation: None, - listener_scroll, listener_animation_frame: Default::default(), prefix, window, @@ -296,6 +265,11 @@ where } fn push(&mut self, state: R) { + let w = window().expect("access to `window`"); + let h = w.history().expect("`window` has access to `history`"); + + // update the scroll position before pushing the new state + update_scroll::(&w, &h); let path = self.full_path(&state); let state = self.create_state(state); @@ -362,6 +336,12 @@ where } fn push(&mut self, state: R) { + let w = window().expect("access to `window`"); + let h = w.history().expect("`window` has access to `history`"); + + // update the scroll position before pushing the new state + update_scroll::(&w, &h); + let path = self.full_path(&state); let state: [f64; 2] = self.create_state(state);