DioxusLabs/dioxus · error

should have access to the Document

Error message

should have access to the Document

What it means

Once the Window is available, `load_document` calls `.document().expect("should have access to the Document")`. The HTML spec permits a Window whose document is null (detached browsing contexts), and stubbed test environments may provide a window without a document; in those cases this second expect panics. If the Window itself were missing, the sibling error at line 284 fires first.

Source

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

}

// 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. Run the renderer in a real browser page rather than a mocked window global
  2. Defer renderer/event work until DOMContentLoaded so a document certainly exists
  3. Fix test environment stubs to provide both `window` and `document`
  4. Cancel pending event work on teardown so it cannot race document detach
Defensive patterns

Strategy: type-guard

Type guard

fn has_document() -> bool {
    web_sys::window().and_then(|w| w.document()).is_some()
}

Prevention

When it happens

Trigger: A window global exists but its document is null: JS test harnesses mocking `window` without `document`, event code running during document teardown/unload races, or exotic embedding contexts that provide a bare Window.

Common situations: jsdom-style mocks in tooling that intercept wasm-bindgen globals; pages being torn down while deferred event code still executes.

Related errors


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