Optimization is not validation or sanitization
SVG is XML that a browser renders as a graphic, but it can also contain references, styling, and behavior. Removing whitespace, editor metadata, or redundant groups may save bytes without deciding whether a file is suitable for a page. Keep three questions separate: is the document valid XML, does it keep the intended picture, and is it acceptable for its destination? An optimizer may simplify a path or remove comments; a sanitizer must decide how to handle script, event handlers, external links, foreignObject, and unsafe URLs. A smaller result is not evidence of safety. When an SVG comes from someone else, review it under a content policy and reopen the output before publishing.
Controlled example: count DOM elements and bytes
Always work on a copy. Start with a simple icon containing two nested groups, a useful title, a description, editor metadata, and four shapes. Before optimization, count DOM elements with the inspector or search for svg, g, path, title, and desc tags; record the file size in bytes as well. Run the optimizer and compare both figures. Empty groups or editor metadata can reasonably disappear, but an accessible title must not vanish without a planned replacement. Keep original and output under different names. The reduction only matters if rendering, purpose, and needed references survive the change.
Confirm rendering, not only the markup
Open original and output in the same browser version and, when the icon belongs in an interface, test it there too. Check outline, fill, stroke, opacity, viewBox, aspect ratio, and clipping at small and large sizes. Zoom in to catch precision changes in curves and thin lines. If the SVG uses an ID for a gradient, clipPath, mask, or marker, inspect that every url(#id) still points to an existing ID. A quick visual comparison can miss a fault visible only on a dark background, at high zoom, or with a particular stylesheet. For production graphics, keep a reference screenshot or repeatable visual check.
Preserve accessible meaning
A decorative icon can be hidden from assistive technology in the context where it is used; a graphic that communicates information needs an accessible name or equivalent alternative. Do not remove title, desc, aria-label, aria-labelledby, or visible text without knowing how meaning will be exposed afterwards. Confirm that aria-labelledby does not name IDs that were removed or renamed. Alternative text should state the function, not internal geometry: “Download report” helps more than “blue down arrow”. Optimization may reduce repeated attributes, but it must not turn an understandable image into a silent one. The accessible QR guide is relevant when SVG is part of a code that also needs instructions outside the image.
Look for active or external content before trusting it
Inspect the XML before embedding it. Signals needing review include script, onload or other on* attributes, xlink:href or href values pointing elsewhere, external images, foreignObject, embedded objects, and unexpected data: references. A network reference can affect privacy, availability, or content policy; active JavaScript and events do not belong in an untrusted static icon. Blocking one tag does not replace a maintained sanitization policy and tests in the real context. Do not paste unknown SVG directly into JSX or HTML with broad permissions. If you only need to show an untrusted file, consider offering it as a download or using suitable isolation.
Limits to test before publishing
Do not apply aggressive changes without a copy when the file contains SMIL or CSS animation, filters, complex masks, embedded fonts, embedded images, or IDs consumed by CSS or JavaScript. Renaming IDs can break selectors, script references, or multiple icons placed on one page; removing a font can change measurements and wrapping. Filters vary between browsers, and a data: image can account for most of the weight even when the XML is small. Check the output in supported browsers and measure the complete load. A large illustration may need redesign or another format for size; security requires separate sanitization and review. The SVG optimization guide explains these decisions, while the privacy guide helps assess received files.