{"record":{"id":"4d4f74046b0d56d3","repo":"DioxusLabs/dioxus","slug":"head-element-to-exist","errorCode":null,"errorMessage":"Head element to exist","messagePattern":"Head element to exist","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/desktop/src/protocol.rs","lineNumber":122,"sourceCode":"    request: &Request<Vec<u8>>,\n    custom_head: Option<String>,\n    custom_index: Option<String>,\n    root_name: &str,\n    headless: bool,\n    edit_state: &WebviewEdits,\n) -> Option<Response<Vec<u8>>> {\n    // If the request is for the root, we'll serve the index.html file.\n    if request.uri().path() != \"/\" {\n        return None;\n    }\n\n    // Load a custom index file if provided\n    let mut index = custom_index.unwrap_or_else(|| DEFAULT_INDEX.to_string());\n\n    // Insert a custom head if provided\n    // We look just for the closing head tag. If a user provided a custom index with weird syntax, this might fail\n    if let Some(head) = custom_head {\n        index.insert_str(index.find(\"</head>\").expect(\"Head element to exist\"), &head);\n    }\n\n    // Inject our module loader by looking for a body tag\n    // A failure mode here, obviously, is if the user provided a custom index without a body tag\n    // Might want to document this\n    index.insert_str(\n        index.find(\"</body>\").expect(\"Body element to exist\"),\n        &module_loader(root_name, headless, edit_state),\n    );\n\n    Response::builder()\n        .header(\"Content-Type\", \"text/html\")\n        .header(\"Access-Control-Allow-Origin\", \"*\")\n        .body(index.into())\n        .ok()\n}\n\n/// Construct the inline script that boots up the page and bridges the webview with rust code.","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/393d190a801ccb441d41923e232289b4f8a5c669/packages/desktop/src/protocol.rs#L104-L140","documentation":"The desktop custom protocol serves index.html and splices the custom head immediately before `</head>`; find(\"</head>\").expect(\"Head element to exist\") panics when a custom head was supplied (DesktopConfig::with_custom_head) but the served index has no exact closing head tag.","triggerScenarios":"Calling DesktopConfig::with_custom_head while a custom index (with_custom_index or the html provided via configuration) lacks `</head>` - including case-mismatched tags like `</HEAD>`, since the search is case-sensitive.","commonSituations":"Hand-written minimal index.html snippets missing a head section; templating that strips the head; refactoring an index and dropping the tag.","solutions":["Add a proper `<head>...</head>` block with the exact lowercase `</head>` closing tag to the custom index","If head injection is not needed, remove the with_custom_head call","Test the custom index contains both `</head>` and `</body>` before passing it in"],"exampleFix":"// before\nlet cfg = DesktopConfig::new().with_custom_index(\"<html><body><div id=main></div></body></html>\".into())\n    .with_custom_head(\"<meta charset='utf-8'>\".into());\n\n// after\nlet cfg = DesktopConfig::new().with_custom_index(\"<html><head></head><body><div id=main></div></body></html>\".into())\n    .with_custom_head(\"<meta charset='utf-8'>\".into());","handlingStrategy":"validation","validationCode":"// Validate custom index before passing it to the desktop config\nfn index_has_head(index: &str) -> bool { index.contains(\"</head>\") }\n\nlet html = std::fs::read_to_string(\"index.html\").unwrap();\nassert!(index_has_head(&html), \"custom index must contain </head>\");","typeGuard":"fn is_valid_custom_index(index: &str) -> bool {\n    index.contains(\"</body>\") && index.contains(\"</head>\")\n}","tryCatchPattern":null,"preventionTips":["Always provide a complete HTML document (html/head/body) as custom index","The tag search is case-sensitive: write lowercase </head> and </body>","Only use with_custom_head when the served index definitely has a head section"],"tags":["desktop","index-html","config","panic","runtime"],"backgroundTag":null,"analyzedSha":"393d190a801ccb441d41923e232289b4f8a5c669","analyzedAt":"2026-08-16T11:27:45.815Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}