vercel/next.js · error
Unknown unit {unit}
Error message
Unknown unit {unit} What it means
After matching a numeric value, parse_length looks up the captured unit string in the UNITS map (in, cm, em, ex, m, mm, pc, pt, px, and empty). If the unit is not found, this error fires. In practice the upstream regex already constrains the unit to the known set or empty, so this is a defensive guard that is rarely reached through normal input.
Source
Thrown at turbopack/crates/turbopack-image/src/process/svg.rs:49
LazyLock::new(|| Regex::new(r#"\swidth=['"]([^%]+?)['"]"#).unwrap());
static HEIGHT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"\sheight=['"]([^%]+?)['"]"#).unwrap());
static VIEW_BOX_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"\sviewBox=['"](.+?)['"]"#).unwrap());
static VIEW_BOX_CONTENT_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^\s*((?:\w|\.|-)+)\s+((?:\w|\.|-)+)\s+((?:\w|\.|-)+)\s+((?:\w|\.|-)+)\s*$")
.unwrap()
});
fn parse_length(len: &str) -> Result<f64> {
let captures = UNIT_REGEX
.captures(len)
.ok_or_else(|| anyhow!("Unknown syntax for length, expected value with unit ({len})"))?;
let val = captures[1].parse::<f64>()?;
let unit = &captures[2];
let unit_scale = UNITS
.get(unit)
.ok_or_else(|| anyhow!("Unknown unit {unit}"))?;
Ok(val * unit_scale)
}
fn parse_viewbox(viewbox: &str) -> Result<(f64, f64)> {
let captures = VIEW_BOX_CONTENT_REGEX
.captures(viewbox)
.ok_or_else(|| anyhow!("Unknown syntax for viewBox ({viewbox})"))?;
let width = parse_length(&captures[3])?;
let height = parse_length(&captures[4])?;
Ok((width, height))
}
fn calculate_by_viewbox(
view_box: (f64, f64),
width: Option<Result<f64>>,
height: Option<Result<f64>>,
) -> Result<(u32, u32)> {
let ratio = view_box.0 / view_box.1;View on GitHub (pinned to 0ae8c72462)
Solutions
- This usually indicates a Turbopack internal bug — file an issue with the SVG that triggered it.
- As a user workaround, normalize the SVG width/height to a plain number (no unit) so the empty-unit path is taken.
Defensive patterns
Strategy: validation
Validate before calling
// Defensive check: ensure the unit is in the supported set.
const UNITS = new Set(['in','cm','em','ex','m','mm','pc','pt','px','']);
function hasSupportedUnit(v) {
const m = /^([0-9.]+(?:e-?\d+)?)(in|cm|em|ex|m|mm|pc|pt|px)?$/.exec((v||'').trim());
return !!m && UNITS.has(m[2]);
}
Prevention
- Normalize SVG dimensions to unitless numbers to sidestep both the syntax and unit checks.
- If this error appears, suspect a Turbopack regression and report the exact SVG.
When it happens
Trigger: Reachable only if the UNIT_REGEX capture group yields a unit token absent from the UNITS phf map — essentially an internal inconsistency or a regex/map desync. Normal malformed input is caught earlier by the 'Unknown syntax for length' error (283).
Common situations: Essentially unreachable via user-supplied SVGs because the regex whitelists units; would only surface from a code change that adds a unit to the regex without updating the UNITS map.
Related errors
- Unknown syntax for length, expected value with unit ({len})
- Unknown syntax for viewBox ({viewbox})
- "url" parameter is valid but image type is not allowed
AI-assisted analysis of vercel/next.js@0ae8c72462 (2026-08-06).
Data as JSON: /api/errors/d1d0e0fcca0e6e84.
Report an issue: GitHub.