dom-utils.ts 539 B

12345678910111213141516
  1. export function applyFaviconToDom(url: string) {
  2. if (typeof document === 'undefined' || !url) return
  3. try {
  4. const next = new URL(url, window.location.href).href
  5. const existing =
  6. document.querySelectorAll<HTMLLinkElement>('link[rel~="icon"]')
  7. if (existing.length === 1 && existing[0].href === next) return
  8. const link = document.createElement('link')
  9. link.rel = 'icon'
  10. link.href = url
  11. existing.forEach((l) => l.remove())
  12. document.head.appendChild(link)
  13. } catch {
  14. // Ignore malformed URLs
  15. }
  16. }