const MALIBU_CDN_BASE = 'https://www.herokucdn.com/malibu'; /** * Reads malibuVersion from window.EMBER_MALIBU_ICON (set by the addon's initializer from the * app's config/environment.js ENV['ember-malibu-icon']). Defaults to 'latest'. */ function getMalibuVersion(): string { if (typeof window === 'undefined') return 'latest'; const config = ( window as Window & { EMBER_MALIBU_ICON?: { malibuVersion?: string } } ).EMBER_MALIBU_ICON; const version = config?.malibuVersion; return version != null && String(version).trim() !== '' ? String(version).trim() : 'latest'; } function getSpriteUrl(): string { return `${MALIBU_CDN_BASE}/${getMalibuVersion()}/sprite.svg`; } let injected: Promise | null = null; /** * Injects the Malibu icon sprite SVG into document.body once. * Idempotent: safe to call from any component; only the first call fetches. * Used by MalibuIcon (so icons work without app rendering MalibuSpriteLoader) * and by MalibuSpriteLoader when explicitly used. */ export function ensureSpriteInjected(): Promise { if (injected !== null) return injected; injected = doInject(); return injected; } async function doInject(): Promise { const spriteUrl = getSpriteUrl(); try { const res = await fetch(spriteUrl); if (!res.ok) throw new Error(`HTTP ${res.status}`); let text = await res.text(); text = text.replace(/^[^\n]*\n/, ''); const parser = new DOMParser(); const doc = parser.parseFromString(text, 'image/svg+xml'); const svg = doc.documentElement; svg.removeAttribute('style'); svg.setAttribute('class', 'malibu-sprite-svg'); svg.style.cssText = 'position: absolute; width: 0; height: 0; overflow: hidden;'; document.body.appendChild(svg); } catch { // Icons will not display; avoid throwing so the app still loads } }