DioxusLabs/dioxus · error

should have access to the Window

Error message

should have access to the Window

What it means

`load_document()` in dioxus-web's event module calls `web_sys::window().expect("should have access to the Window")`. `web_sys::window()` is None wherever no Window global exists - web workers, Node/SSR processes, non-browser wasm runtimes - so the renderer panics the first time its event machinery needs the document in such a context.

Source

Thrown at packages/web/src/events/mod.rs:284

    raw: Event,
    element: Element,
}

// todo: some of these events are being casted to the wrong event type.
// We need tests that simulate clicks/etc and make sure every event type works.
pub(crate) fn virtual_event_from_websys_event(
    event: web_sys::Event,
    target: Element,
) -> PlatformEventData {
    PlatformEventData::new(Box::new(GenericWebSysEvent {
        raw: event,
        element: target,
    }))
}

pub(crate) fn load_document() -> Document {
    web_sys::window()
        .expect("should have access to the Window")
        .document()
        .expect("should have access to the Document")
}

View on GitHub (pinned to 393d190a80)

Solutions

  1. Launch the web renderer only from the browser main thread (wasm-bindgen start inside an HTML page)
  2. Gate web-only code with `#[cfg(target_arch = "wasm32")]` and select the correct renderer per platform
  3. In tests or SSR, use the server/memory renderer instead of dioxus-web
  4. Check `web_sys::window().is_some()` before entering code paths that depend on web event APIs

Example fix

// before: compiled for every target, panics on the server/worker
fn setup_events() { /* dioxus-web event wiring */ }
// after: only compiled for the browser
#[cfg(target_arch = "wasm32")]
fn setup_events() { /* dioxus-web event wiring */ }
Defensive patterns

Strategy: type-guard

Type guard

fn has_browser_window() -> bool {
    web_sys::window().is_some()
}

Prevention

When it happens

Trigger: Loading or running dioxus-web inside a web worker (no Window global); executing web-renderer event code during server-side rendering because the web crate was linked into the server build; wasm-bindgen-test runs outside a browser page.

Common situations: Fullstack apps without cfg-gating renderer-specific code; experiments moving UI into workers; feature misconfiguration pulling dioxus::web into a non-browser target.

Related errors


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