Does SVGO’s default enable of removeViewBox break repeatable SVG optimization?
0 reputation · 14 Apr 2024, 00:00 UTC
0 reputation · 14 Apr 2024, 00:00 UTC
Teams aiming for a repeatable SVG optimization workflow often commit an SVGO configuration file (e.g., .svgorc.js) to lock the exact plugin set and options, ensuring identical output across local machines and CI pipelines.
One unresolved design decision in SVGO concerns whether the removeViewBox plugin should be enabled by default; changing this default could affect responsive SVG scaling and break existing builds that rely on the viewBox attribute.
Should the removeViewBox plugin remain enabled by default to preserve repeatability, or should it be disabled to avoid unintentionally altering viewBox‑dependent layouts? How does toggling this default impact the consistency of optimized SVGs across different environments? What guidance should teams follow when deciding whether to override the default in their shared configuration?
32275 reputation · 14 Apr 2024, 01:38 UTC
The removeViewBox plugin being enabled by default in SVGO can indeed introduce non-determinism into repeatable SVG optimization workflows, particularly for responsive SVGs. The core issue isn't whether the plugin itself works—it functions as designed—but whether its default state aligns with the goal of predictable, environment-consistent output.
In SVGO v2.x and later, the removeViewBox plugin is enabled by default. It removes the viewBox attribute when both width and height are present, under the assumption that the SVG is not responsive. This is documented behavior, and the plugin logic is straightforward: if an SVG has explicit dimensions and no need for scaling, the viewBox can be safely stripped to reduce file size.
For teams committing an SVGO configuration file to version control, the default setting matters because it determines the baseline optimization behavior. If the config doesn't explicitly set removeViewBox, the plugin remains active, and its behavior becomes part of the optimization contract across environments.
The risk to repeatability arises in two scenarios:
viewBox for scaling (e.g., inline SVGs in HTML that must adapt to container size), enabling removeViewBox by default will strip it, causing visual breakage that may not surface in all environments.However, SVGO's defaults have remained stable across recent versions, so the primary concern is correctness, not consistency across SVGO versions.
To ensure repeatable optimization, teams should:
plugins: [{ removeViewBox: false }] to your .svgorc.js if your SVGs are responsive or if you want to preserve viewBox for future-proofing.removeViewBox is set to false (or true), so future maintainers understand the rationale.Run the following to inspect current defaults:
npx svgo --show-plugins | grep removeViewBoxThis will show whether the plugin is enabled by default in your current SVGO version. Then test with a responsive SVG:
npx svgo input.svg -o output.svg && cat output.svg | grep viewBoxIf viewBox is missing and your SVG is responsive, you've confirmed the issue.
The removeViewBox default doesn't inherently break repeatability—it provides a sensible optimization for non-responsive SVGs. The problem is that teams may not realize it's active, leading to inconsistent visual results. The solution is explicit configuration, not disabling the plugin universally. For maximum safety in shared workflows, set removeViewBox: false and rely on other plugins for size optimization.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.