DioxusLabs/dioxus · error
should be run in a context with a `Window` object (dioxus ca
Error message
should be run in a context with a `Window` object (dioxus cannot be run from a web worker)
What it means
`ScrollPosition::scroll_to` - used by the router to restore scroll after navigation - wraps `window.scrollTo(x, y)` in a requestAnimationFrame callback and re-fetches `web_sys::window()` with an expect whose message states the constraint outright: dioxus cannot run from a web worker. In any window-less context (worker, SSR), scroll restoration panics.
Source
Thrown at packages/web/src/history.rs:363
pub x: f64,
pub y: f64,
}
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,View on GitHub (pinned to 393d190a80)
Solutions
- Keep the router and its scroll restoration on the browser main thread
- Disable scroll restoration (construct the history with do_scroll_restoration=false) in environments where scrolling cannot work
- cfg-gate web routing code out of server builds
Example fix
// before: scroll restoration enabled, panics in worker contexts let history = WebHistory::new(None, true); // after: disable it where the Window/rAF path cannot run let history = WebHistory::new(None, false);
Defensive patterns
Strategy: type-guard
Type guard
fn can_restore_scroll() -> bool {
web_sys::window().is_some()
} Prevention
- Never host dioxus-web in a web worker - the message itself states this constraint
- Disable scroll restoration (do_scroll_restoration=false) in any environment where the Window/rAF path cannot run
- cfg-gate web routing code out of server builds so scroll restoration cannot execute there
When it happens
Trigger: Router scroll restoration (or a direct scroll_to call) executing in a web worker; scroll restore triggered after the browsing context is gone.
Common situations: The same misconfigurations that break WebHistory construction: worker-hosted UI, SSR builds linking web renderer code, headless tests touching navigation.
Related errors
- access to `window`
- should register `requestAnimationFrame` OK
- should have access to the Window
- `history` can set scroll restoration
- A router must be present to use navigator
AI-assisted analysis of DioxusLabs/dioxus@393d190a80 (2026-08-16).
Data as JSON: /api/errors/e376c0a398b8fd97.
Report an issue: GitHub.