DioxusLabs/dioxus · error

should register `requestAnimationFrame` OK

Error message

should register `requestAnimationFrame` OK

What it means

After re-obtaining the window, `scroll_to` registers its scroll callback with `.request_animation_frame(...).expect("should register `requestAnimationFrame` OK")`. Registration fails when the API is unavailable or the context rejects it - rAF is missing in certain worker/sandboxed contexts and old embedded engines, or the document/iframe is being torn down - and the router panics while restoring scroll.

Source

Thrown at packages/web/src/history.rs:365

}

impl ScrollPosition {
    pub(crate) fn of_window(window: &Window) -> Self {
        Self {
            x: window.scroll_x().unwrap_or_default(),
            y: window.scroll_y().unwrap_or_default(),
        }
    }

    pub(crate) fn scroll_to(&self, window: Window) {
        let Self { x, y } = *self;
        let f = Closure::wrap(
            Box::new(move || window.scroll_to_with_x_and_y(x, y)) as Box<dyn FnMut()>
        );
        web_sys::window()
            .expect("should be run in a context with a `Window` object (dioxus cannot be run from a web worker)")
            .request_animation_frame(&f.into_js_value().unchecked_into())
            .expect("should register `requestAnimationFrame` OK");
    }
}

pub(crate) fn replace_state_with_url(
    history: &History,
    value: &[f64; 2],
    url: Option<&str>,
) -> Result<(), JsValue> {
    let position = js_sys::Array::new();
    position.push(&JsValue::from(value[0]));
    position.push(&JsValue::from(value[1]));
    history.replace_state_with_url(&position, "", url)
}

pub(crate) fn push_state_and_url(
    history: &History,
    value: &[f64; 2],
    url: String,

View on GitHub (pinned to 393d190a80)

Solutions

  1. Ensure navigation and scroll-restoration code runs on the live main thread with an active document
  2. Cancel or debounce navigation-driven scroll work during page teardown
  3. Upgrade the webview/browser so requestAnimationFrame is reliably available
Defensive patterns

Strategy: validation

Validate before calling

// verify rAF is available before navigation-driven scroll work
let raf_ok = web_sys::window()
    .map(|w| js_sys::Reflect::get(w.as_ref(), &wasm_bindgen::JsValue::from_str("requestAnimationFrame")).is_ok())
    .unwrap_or(false);
if !raf_ok { /* skip scroll restoration */ }

Prevention

When it happens

Trigger: Scroll restoration running where requestAnimationFrame does not exist (worker contexts, legacy webviews, restricted sandboxed iframes); a navigation racing page unload so rAF registration throws.

Common situations: SPA navigation firing as the user closes the tab; old embedded webview targets; test engines with partial browser APIs.

Related errors


AI-assisted analysis of DioxusLabs/dioxus@393d190a80 (2026-08-16). Data as JSON: /api/errors/e0d26553b648545a. Report an issue: GitHub.