{"record":{"id":"8a134e9b5c240c38","repo":"getzola/zola","slug":"failed-to-convert-bytes-to-string","errorCode":null,"errorMessage":"Failed to convert bytes to string : {}","messagePattern":"Failed to convert bytes to string : (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"components/site/src/minify.rs","lineNumber":13,"sourceCode":"use errors::{Result, bail};\nuse minify_html::{Cfg, minify};\n\npub fn html(html: String) -> Result<String> {\n    let mut cfg = Cfg::new();\n    cfg.keep_html_and_head_opening_tags = true;\n    cfg.minify_css = true;\n    cfg.minify_js = false;\n\n    let minified = minify(html.as_bytes(), &cfg);\n    match std::str::from_utf8(&minified) {\n        Ok(result) => Ok(result.to_string()),\n        Err(err) => bail!(\"Failed to convert bytes to string : {}\", err),\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    // https://github.com/getzola/zola/issues/1292\n    #[test]\n    fn can_minify_html() {\n        let input = r#\"\n<!doctype html>\n<html>\n<head>\n  <meta charset=\"utf-8\">\n</head>\n<body>\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/getzola/zola/blob/61d30828217957120309db657950224edf9707e2/components/site/src/minify.rs#L1-L31","documentation":"The `html` minification helper in components/site/src/minify.rs minifies HTML via the `minify` crate and then converts the minified bytes back to a String with `std::str::from_utf8`. If the resulting bytes are not valid UTF-8, it bails with this message including the Utf8Error. This indicates the minifier produced output that is not valid UTF-8 for the given input.","triggerScenarios":"Calling the `html` minify function with input whose minified output contains invalid UTF-8 byte sequences — typically caused by non-UTF-8 input encodings or a minifier bug on multibyte content.","commonSituations":"Sites with HTML files saved in Latin-1/Windows-1252 or containing malformed byte sequences; certain multibyte characters interacting with the minifier.","solutions":["Re-save the HTML input file as UTF-8 without BOM","Find the offending byte sequence from the embedded Utf8Error (it includes the byte index) and fix that character in the source file","Verify the file has no mixed encodings (e.g. `file` or `iconv` to detect)","If caused by the minify crate, pin/upgrade the minify dependency"],"exampleFix":"// before\n$ cat page.html | minify  # file saved as Windows-1252\n// after\n$ iconv -f WINDOWS-1252 -t UTF-8 page.html > page.html","handlingStrategy":"try-catch","validationCode":"if let Err(e) = std::str::from_utf8(html.as_bytes()) {\n    return Err(anyhow!(\"input is not UTF-8 at byte {}: {e}\", e.valid_up_to()));\n}","typeGuard":"fn is_valid_utf8(bytes: &[u8]) -> bool { std::str::from_utf8(bytes).is_ok() }","tryCatchPattern":"match minify_to_string(html) {\n    Ok(out) => write(out),\n    Err(e) if e.to_string().contains(\"Failed to convert bytes\") => {\n        // fall back to unminified input\n        write(html.to_string());\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Store all site HTML/CSS/JS as UTF-8 without BOM","Validate file encodings in CI (e.g. `iconv` or a lint step)","Keep the minify dependency updated for multibyte handling fixes"],"tags":["rust","encoding","utf-8","minification"],"backgroundTag":"invalid-utf8","analyzedSha":"61d30828217957120309db657950224edf9707e2","analyzedAt":"2026-09-03T14:39:09.727Z","contentChangedAt":"2026-09-03T14:39:09.727Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}