A JavaScript polyfill library that enables external SVG spritemap support across all browsers, including legacy versions.
npx @tessl/cli install tessl/npm-svg4everybody@2.1.0SVG for Everybody is a JavaScript polyfill library that enables external SVG spritemap support across all browsers, including legacy versions of Internet Explorer, Safari, and Edge. It allows developers to use external SVG files with the <use> element by automatically detecting when external content is not natively supported and providing fallbacks through dynamic content injection or image replacements.
npm install svg4everybodyimport svg4everybody from "svg4everybody";const svg4everybody = require("svg4everybody");<script src="/path/to/svg4everybody.js"></script>
<!-- svg4everybody is now available globally --><script src="/path/to/svg4everybody.legacy.js"></script>
<!-- For IE 6-8 support with image fallbacks and additional options -->Standard vs Legacy Versions:
svg4everybody.js): Supports modern browsers and IE 9+, includes polyfill, validate, and attributeName optionssvg4everybody.legacy.js): Supports IE 6-8 with image fallbacks, includes all standard options plus fallback and nosvg options// Basic usage - auto-detect browser needs and enable polyfill
svg4everybody();
// Call after DOM is ready or when new SVG elements are added
document.addEventListener('DOMContentLoaded', function() {
svg4everybody();
});<!-- External SVG spritemap -->
<svg role="img" title="CodePen">
<use xlink:href="map.svg#codepen"/>
</svg>
<svg role="img" title="YouTube">
<use xlink:href="map.svg#youtube"/>
</svg><!-- map.svg -->
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="codepen" viewBox="0 0 64 64">
<title>CodePen</title>
<path d="..."/>
</symbol>
<symbol id="youtube" viewBox="0 0 64 64">
<title>YouTube</title>
<path d="..."/>
</symbol>
</svg>Initialize and run the SVG external content polyfill.
/**
* Main polyfill function that enables SVG external content support
* @param rawopts - Optional configuration object
*/
function svg4everybody(rawopts?: SVGForEverybodyOptions): void;interface SVGForEverybodyOptions {
/** Whether to activate the polyfill for external SVG content (auto-detected if not specified) */
polyfill?: boolean;
/** Custom validation function to determine which <use> elements to process */
validate?: (src: string, svg: Element, use: Element) => boolean;
/** Custom fallback function for generating image replacement URLs (LEGACY VERSION ONLY) */
fallback?: (src: string, svg: Element, use: Element) => string;
/** Forces image fallbacks instead of SVG (LEGACY VERSION ONLY, auto-detected for IE 6-8) */
nosvg?: boolean;
/** Alternative attribute name to check for SVG reference (in addition to xlink:href and href) */
attributeName?: string;
}svg4everybody({
validate: function(src, svg, use) {
// Only process sprites from the /sprites/ directory
return src.startsWith('/sprites/');
}
});// IMPORTANT: This requires svg4everybody.legacy.js, not the standard version
svg4everybody({
fallback: function(src, svg, use) {
// Custom fallback image logic
return '/images/svg-fallback.png';
}
});// IMPORTANT: This requires svg4everybody.legacy.js, not the standard version
svg4everybody({
nosvg: true, // Force image fallbacks for all browsers
fallback: function(src, svg, use) {
// Convert map.svg#icon to map.svg.icon.png
return src.replace(/\?[^#]+/, '').replace('#', '.').replace(/^\./, '') + '.png';
}
});svg4everybody({
polyfill: true // Force polyfill activation regardless of browser detection
});svg4everybody({
attributeName: 'data-href' // Check data-href attribute in addition to xlink:href and href
});svg4everybody.legacy.js instead of the standard version<head> to properly shiv <svg> and <use> elementsX-UA-Compatible to ie=edge for best results<use/> elements with trailing slash<!-- Use title attributes and ARIA roles -->
<svg role="img" title="Find me on Twitter">
<use xlink:href="map.svg#twitter"/>
</svg>
<!-- For decorative icons -->
<svg role="presentation">
<use xlink:href="map.svg#decorative-icon"/>
</svg>By default, fallback images point to a PNG file in the same location as the SVG, with the # hash replaced by a . dot and appended with a .png extension:
map.svg#codepen → map.svg.codepen.pngsprites.svg#icon → sprites.svg.icon.png/** Configuration options for svg4everybody function */
interface SVGForEverybodyOptions {
polyfill?: boolean;
validate?: ValidationFunction;
fallback?: FallbackFunction;
nosvg?: boolean;
attributeName?: string;
}
/** Validation function signature */
type ValidationFunction = (
src: string, // Current xlink:href or href attribute value
svg: Element, // Current SVG element
use: Element // Current USE element
) => boolean; // Return true to process, false to skip
/** Fallback function signature (legacy version only) */
type FallbackFunction = (
src: string, // Current xlink:href or href attribute value
svg: Element, // Current SVG element
use: Element // Current USE element
) => string; // Return URL to fallback image