{"record":{"id":"3938b609cb852853","repo":"iced-rs/iced","slug":"append-canvas-to-html-body","errorCode":null,"errorMessage":"Append canvas to HTML body","messagePattern":"Append canvas to HTML body","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"winit/src/lib.rs","lineNumber":374,"sourceCode":"                                    let document = window.document().unwrap();\n                                    let body = document.body().unwrap();\n\n                                    let target = target.and_then(|target| {\n                                        body.query_selector(&format!(\"#{target}\"))\n                                            .ok()\n                                            .unwrap_or(None)\n                                    });\n\n                                    match target {\n                                        Some(node) => {\n                                            let _ = node.replace_with_with_node_1(&canvas).expect(\n                                                &format!(\"Could not replace #{}\", node.id()),\n                                            );\n                                        }\n                                        None => {\n                                            let _ = body\n                                                .append_child(&canvas)\n                                                .expect(\"Append canvas to HTML body\");\n                                        }\n                                    };\n                                }\n\n                                self.process_event(\n                                    event_loop,\n                                    Event::WindowCreated {\n                                        id,\n                                        window: Arc::new(window),\n                                        exit_on_close_request,\n                                        make_visible: visible,\n                                        on_open,\n                                    },\n                                );\n                            }\n                            Control::Exit => {\n                                self.process_event(event_loop, Event::Exit);\n                                event_loop.exit();","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/iced-rs/iced/blob/d146509d893945de5c304cfaf4301335eee278bd/winit/src/lib.rs#L356-L392","documentation":"On wasm32, after obtaining the window canvas the runtime appends it to the document body and expects the DOM append_child call to succeed. append_child returns an Err (HierarchyRequestError or similar) when the body element is missing or the node cannot be attached; iced cannot display anything without the canvas in the DOM, so it panics.","triggerScenarios":"Calling run on wasm when document.body() is None (script executed before <body> exists or in a non-HTML context), or when the canvas node cannot be a child of body (wrong node type/already-attached hierarchy issue).","commonSituations":"WASM module loaded in <head> without defer, so body doesn't exist yet; using a custom loader that runs before the document finishes parsing; page contexts where body was replaced during startup.","solutions":["Load the wasm entry point after the DOM is ready: use <script type=\"module\">, defer, or wait for the load event before calling the iced runner.","Ensure the HTML document has a valid <body> element at startup.","If a #container node lookup fails upstream, verify the element id in HTML matches what the app queries before the append_child fallback runs.","Pin compatible iced/winit versions; the DOM mounting logic changed across releases."],"exampleFix":"// before (html)\n<head><script src=\"app.js\"></script></head>\n// after (html)\n<body><script type=\"module\" src=\"app.js\"></script></body>","handlingStrategy":"validation","validationCode":"// only mount after the document body exists\nif (document.readyState === \"loading\") {\n  await new Promise(r => document.addEventListener(\"DOMContentLoaded\", r));\n}\nif (!document.body) throw new Error(\"document.body missing; cannot mount canvas\");","typeGuard":"function bodyReady(): boolean {\n  return document.readyState !== \"loading\" && document.body !== null;\n}","tryCatchPattern":"try {\n  body.appendChild(canvas);\n} catch (e) {\n  console.error(\"Canvas mount failed; is <body> present and parsed?\", e);\n}","preventionTips":["Load the wasm entry with type=\"module\" or defer so <body> exists.","Never bootstrap the GUI from <head> without deferral.","Confirm the container element id matches the app's query before relying on the append fallback.","Pin compatible iced/winit versions for the web target."],"tags":["wasm","web","dom","winit","panic"],"backgroundTag":"dom-append-failed","analyzedSha":"d146509d893945de5c304cfaf4301335eee278bd","analyzedAt":"2026-09-11T16:54:14.229Z","contentChangedAt":"2026-09-11T16:54:14.229Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}