diff --git a/scripts/lib-franklin.js b/scripts/lib-franklin.js index 80b042e064..bc01d53674 100644 --- a/scripts/lib-franklin.js +++ b/scripts/lib-franklin.js @@ -157,79 +157,30 @@ export function toCamelCase(name) { return toClassName(name).replace(/-([a-z])/g, (g) => g[1].toUpperCase()); } -const ICONS_CACHE = {}; /** - * Replace icons with inline SVG and prefix with codeBasePath. - * @param {Element} [element] Element containing icons + * Add for icon, prefixed with codeBasePath and optional prefix. + * @param {span} [element] span element with icon classes + * @param {string} [prefix] prefix to be added to icon the src */ -export async function decorateIcons(element) { - // Prepare the inline sprite - let svgSprite = document.getElementById('franklin-svg-sprite'); - if (!svgSprite) { - const div = document.createElement('div'); - div.innerHTML = ''; - svgSprite = div.firstElementChild; - document.body.append(div.firstElementChild); - } +export function decorateIcon(span, prefix = '') { + const iconName = Array.from(span.classList).find((c) => c.startsWith('icon-')).substring(5); + const img = document.createElement('img'); + img.dataset.iconName = iconName; + img.src = `${prefix}/icons/${iconName}.svg`; + img.loading = 'lazy'; + + span.append(img); +} - // Download all new icons +/** + * Add for icons, prefixed with codeBasePath and optional prefix. + * @param {Element} [element] Element containing icons + * @param {string} [prefix] prefix to be added to icon the src + */ +export function decorateIcons(element, prefix = '') { const icons = [...element.querySelectorAll('span.icon')]; - await Promise.all(icons.map(async (span) => { - const iconName = Array.from(span.classList).find((c) => c.startsWith('icon-')).substring(5); - if (!ICONS_CACHE[iconName]) { - ICONS_CACHE[iconName] = true; - try { - const response = await fetch(`${window.hlx.codeBasePath}/icons/${iconName}.svg`); - if (!response.ok) { - ICONS_CACHE[iconName] = false; - return; - } - // Styled icons don't play nice with the sprite approach because of shadow dom isolation - // and same for internal references - const svg = await response.text(); - if (svg.match(/(