{"record":{"id":"9b3bed8f12ead1eb","repo":"slint-ui/slint","slug":"wasm-interpreter-could-not-retrieve-dom-window","errorCode":null,"errorMessage":"wasm-interpreter: Could not retrieve DOM window","messagePattern":"wasm-interpreter: Could not retrieve DOM window","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"api/wasm-interpreter/src/lib.rs","lineNumber":387,"sourceCode":"thread_local!(\n    static NEXT_CANVAS_ID: Rc<RefCell<Option<String>>> = Default::default();\n);\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))","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/api/wasm-interpreter/src/lib.rs#L369-L405","documentation":"The WASM interpreter's init retrieves the browser DOM window via web_sys::window() and panics if it is None, because the backend needs the window to locate the HTML canvas element. web_sys::window() returns None when the code is not running in a browser main context, so this panic indicates the runtime environment fundamentally lacks DOM access.","triggerScenarios":"Calling the WASM interpreter's exported init() from a non-browser JS context: Node.js, a web worker without DOM access, or a service worker.","commonSituations":"Running the slint-interpreter WASM bundle under Node in unit tests, loading it in a Web Worker expecting it to render, or misconfigured bundler targets (node target instead of web).","solutions":["Run the WASM module in a browser main thread (or an environment exposing `window`/`document`), not Node.js or a bare worker.","If using a worker architecture, do DOM/canvas work on the main thread and only offload computation to the worker.","Check the bundler/loader target: use a web target (e.g. wasm-bindgen --target web) so init runs on the main thread.","In tests, use a headless browser (e.g. playwright/wasm-bindgen-test in browser mode) instead of Node.","Polyfills that fake `window` are insufficient — the backend needs a real DOM canvas; use OffscreenCanvas-capable integration only if the backend supports it."],"exampleFix":"// before (Node test)\nconst slint = await import('./slint_interpreter.js');\nawait slint.default();\n// after (browser main thread via wasm-bindgen web target)\n<script type=\"module\">\n  import init from './slint_interpreter.js';\n  await init(); // window/document available\n</script>","handlingStrategy":"fallback","validationCode":"// JS host, before calling init()\nif (typeof window === 'undefined' || typeof document === 'undefined') {\n    throw new Error('slint-interpreter requires a browser main thread; got ' +\n      (typeof window === 'undefined' ? 'no window' : 'no document'));\n}","typeGuard":"// JS host\nfunction hasDomAccess() {\n  try { return typeof window !== 'undefined' && !!window.document && !!window.document.createElement; }\n  catch { return false; }\n}","tryCatchPattern":"// JS host\ntry {\n  await init();\n} catch (e) {\n  if (String(e).includes('DOM window')) {\n    console.error('Load the WASM interpreter in a browser main thread, not Node/worker.');\n  }\n  throw e;\n}","preventionTips":["Always load the WASM interpreter on the browser main thread.","Use wasm-bindgen --target web and a module script, not a Node require.","Do not run DOM-dependent Slint init inside Web Workers or service workers.","In CI, test with a headless browser rather than Node.","Ensure the canvas element with the expected id exists before init runs."],"tags":["wasm","browser","dom","panic","init"],"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"}