getzola/zola · error

Invalid dimensions: SVG width/height and viewbox not set.

Error message

Invalid dimensions: SVG width/height and viewbox not set.

What it means

`ImageMetaResponse::new_svg` extracts dimensions from an SVG file via SvgMetadata. An SVG can declare size via explicit width/height attributes, a viewBox, or neither (relying on CSS). When neither width/height nor a viewBox is present there is no intrinsic size, so the constructor fails with this error.

Source

Thrown at components/imageproc/src/meta.rs:74

}

#[derive(Debug, Serialize, Eq, PartialEq)]
pub struct ImageMetaResponse {
    pub width: u32,
    pub height: u32,
    pub format: Option<&'static str>,
    pub mime: Option<&'static str>,
    pub description: Option<String>,
    pub created: Option<String>,
}

impl ImageMetaResponse {
    fn new_svg(path: &Path) -> Result<Self> {
        let img = SvgMetadata::parse_file(path)?;
        let (w, h) = match (img.width(), img.height(), img.view_box()) {
            (Some(w), Some(h), _) => Ok((w, h)),
            (_, _, Some(view_box)) => Ok((view_box.width, view_box.height)),
            _ => Err(anyhow!("Invalid dimensions: SVG width/height and viewbox not set.")),
        }?;
        Ok(Self {
            width: w as u32,
            height: h as u32,
            format: Some("svg"),
            mime: Some("text/svg+xml"),
            // SVG files have these fields, but we'd need a more comprehensive parser to read them.
            description: None,
            created: None,
        })
    }

    fn new_avif(path: &Path) -> Result<Self> {
        let avif_data = read_avif(&mut BufReader::new(fs::File::open(path)?))?;
        let meta = avif_data.primary_item_metadata()?;
        Ok(Self {
            width: meta.max_frame_width.get(),
            height: meta.max_frame_height.get(),

View on GitHub (pinned to 61d3082821)

Solutions

  1. Add a `viewBox="0 0 W H"` attribute to the SVG root element (preferred, keeps it responsive)
  2. Or add explicit numeric `width` and `height` attributes to the `<svg>` element
  3. If the file cannot be edited, replace it with a properly sized SVG

Example fix

// before
<svg xmlns="http://www.w3.org/2000/svg">
// after
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
Defensive patterns

Strategy: validation

Validate before calling

import re
SVG_VIEWBOX = re.compile(rb'viewBox\s*=\s*"\s*[\d.]+[\s,]+[\d.]+[\s,]+[\d.]+[\s,]+[\d.]+')
SVG_SIZE = re.compile(rb'<svg[^>]*\swidth\s*=')
def svg_has_dimensions(data: bytes) -> bool:
    return bool(SVG_VIEWBOX.search(data) and (SVG_SIZE.search(data)))

Type guard

fn svg_is_sizable(root: &svg::node::element::SVG) -> bool {
    root.get_attr("viewBox").is_some()
        || (root.get_attr("width").is_some() && root.get_attr("height").is_some())
}

Try / catch

match ImageMetaResponse::new_svg(path) {
    Ok(meta) => use_meta(meta),
    Err(e) if e.to_string().contains("Invalid dimensions: SVG") => {
        log::warn!("{} lacks intrinsic size; skipping", path.display());
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling image metadata inspection (new_svg) on an .svg file whose root `<svg>` element has neither `width`/`height` attributes nor a `viewBox` attribute.

Common situations: Hand-authored or designer-exported SVGs sized purely with CSS (e.g. width:100%) or with percentage width/height only; icons generated by tools that omit viewBox; minified SVGs stripped of attributes.

Related errors


AI-assisted analysis of getzola/zola@61d3082821 (2026-09-03). Data as JSON: /api/errors/6665f5d402c45e9f. Report an issue: GitHub.