Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions packages/hub-ui/src/client/components/icons/IconifyIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,36 @@ const iconifyParsed = computed(() => {
})

const iconifyLoaded = ref<string | undefined>(undefined)
watchEffect(async () => {
if (!iconifyParsed.value) {
iconifyLoaded.value = undefined
const failed = ref(false)
watchEffect(async (onCleanup) => {
let active = true
onCleanup(() => {
active = false
})
iconifyLoaded.value = undefined
failed.value = false
if (!iconifyParsed.value)
return
}
try {
iconifyLoaded.value = await getIconifySvg(iconifyParsed.value.collection, iconifyParsed.value.icon)
const svg = await getIconifySvg(iconifyParsed.value.collection, iconifyParsed.value.icon)
if (active)
iconifyLoaded.value = svg
}
catch {
// A failed icon fetch (offline / flaky CDN) should degrade to a blank icon,
// not throw out of the async effect and crash the surrounding panel.
iconifyLoaded.value = undefined
if (active)
failed.value = true
}
})
</script>

<template>
<svg v-if="failed" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true" class="w-full h-full">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better use UnoCSS icons instead of inline SVG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, should be fixed now

<rect x="3" y="3" width="18" height="18" rx="3" />
<path d="M12 7v6m0 3v1" />
</svg>
<div
v-if="iconifyParsed"
v-else-if="iconifyParsed"
aria-hidden="true"
v-html="iconifyLoaded"
/>
Comment thread
dvcolomban marked this conversation as resolved.
<img
Expand Down
16 changes: 11 additions & 5 deletions packages/hub-ui/src/client/utils/iconify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ export async function getIconifySvg(collection: string, icon: string) {

async function _get() {
const url = `https://api.iconify.design/${collection}/${icon}.svg?color=currentColor&width=100%`
// Bound the request so a stalled connection (offline / flaky CDN / firewall
// black-holing the host) rejects instead of hanging forever; the caller
// already degrades a rejected fetch to a blank icon.
const svg = await fetch(url, { signal: AbortSignal.timeout(10_000) }).then(res => res.text())
return purify.sanitize(svg)
const response = await fetch(url, { signal: AbortSignal.timeout(10_000) })
if (!response.ok)
throw new Error(`Iconify request failed: ${response.status}`)
const svg = purify.sanitize(await response.text())
const svgDocument = new DOMParser().parseFromString(svg, 'image/svg+xml')
if (svgDocument.documentElement.localName !== 'svg'
|| svgDocument.querySelector('parsererror')
|| !svgDocument.querySelector('path, circle, ellipse, rect, line, polyline, polygon, text, use, image')) {
throw new Error('Iconify returned an invalid SVG')
}
return svg
}
}
Loading