{"record":{"id":"5976637a8c3c4bfe","repo":"slint-ui/slint","slug":"wasm-interpreter-could-not-retrieve-dom-document","errorCode":null,"errorMessage":"wasm-interpreter: Could not retrieve DOM document","messagePattern":"wasm-interpreter: Could not retrieve DOM document","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"api/wasm-interpreter/src/lib.rs","lineNumber":389,"sourceCode":");\n\n#[wasm_bindgen(start)]\npub fn init() -> Result<(), JsValue> {\n    #[cfg(feature = \"console_error_panic_hook\")]\n    console_error_panic_hook::set_once();\n    let backend = i_slint_backend_winit::Backend::builder()\n        .with_spawn_event_loop(true)\n        .with_window_attributes_hook(|mut attrs| {\n            NEXT_CANVAS_ID.with(|next_id| {\n                if let Some(canvas_id) = next_id.borrow_mut().take() {\n                    use i_slint_backend_winit::winit::platform::web::WindowAttributesExtWebSys;\n\n                    use wasm_bindgen::JsCast;\n\n                    let html_canvas = web_sys::window()\n                        .expect(\"wasm-interpreter: Could not retrieve DOM window\")\n                        .document()\n                        .expect(\"wasm-interpreter: Could not retrieve DOM document\")\n                        .get_element_by_id(&canvas_id)\n                        .unwrap_or_else(|| {\n                            panic!(\n                                \"wasm-interpreter: Could not retrieve existing HTML Canvas element '{}'\",\n                                canvas_id\n                            )\n                        })\n                        .dyn_into::<web_sys::HtmlCanvasElement>()\n                        .unwrap_or_else(|_| {\n                            panic!(\n                                \"winit backend: Specified DOM element '{}' is not a HTML Canvas\",\n                                canvas_id\n                            )\n                        });\n                    attrs = attrs\n                        .with_canvas(Some(html_canvas))\n                        // Don't activate the window by default, as that will cause the page to scroll,\n                        // ignoring any existing anchors.","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/api/wasm-interpreter/src/lib.rs#L371-L407","documentation":"The wasm-interpreter crate's `init` function calls `web_sys::window().document()`, which returns None when the wasm code is not running in a browser context that exposes a DOM document. The library panics because `init` must bind to an HTML canvas element that only exists in a web page's DOM. This happens when the compiled wasm runs outside a browser, or in workers/iframes without document access.","triggerScenarios":"Calling `slint_interpreter::init()` (or the generated JS entry point that calls it) in a WebAssembly context where `window().document()` returns None: running in a web worker, service worker, Node.js wasm runtime, or a headless/non-browser embedding.","commonSituations":"Running unit tests for the wasm crate under Node.js or wasm-bindgen-test in a non-DOM runner; embedding the interpreter wasm in a server-side or CLI context; initializing from a worker thread instead of the main browser thread; loading the module before the page DOM is available.","solutions":["Only call init() from the main browser thread where `window` and `document` are available; do the init from JS glue loaded into a real page.","If using a worker, proxy canvas/UI initialization to the main thread via postMessage and only call Slint APIs there.","For headless testing, use a DOM-emulating runner (e.g. wasm-bindgen-test with a browser runner) instead of Node.","Check `web_sys::window().and_then(|w| w.document()).is_some()` before calling init and skip or route init accordingly."],"exampleFix":"// before (worker or Node test)\nslint_interpreter::init(); // panics: no DOM document\n\n// after: guard on browser context\nif web_sys::window().and_then(|w| w.document()).is_some() {\n    slint_interpreter::init();\n} else {\n    // defer to main thread / browser-only path\n}","handlingStrategy":"validation","validationCode":"const has_dom = () => typeof web_sys::window === 'function' && web_sys::window().map(|w| w.document().is_some()).unwrap_or(false);\nif (!has_dom()) { /* route init to main thread or skip */ }","typeGuard":"fn has_dom_document() -> bool {\n    web_sys::window().and_then(|w| w.document()).is_some()\n}","tryCatchPattern":null,"preventionTips":["Never call Slint wasm init from web workers, service workers, or Node.js","Run wasm tests with a browser-based runner (wasm-bindgen-test in a real browser)","Wrap init in a context check before any DOM-touching API"],"tags":["wasm","browser","dom","web-sys"],"backgroundTag":"unsupported-platform","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}