Skip to content

Commit cb44eed

Browse files
committed
fix: tolerant nullish icon name, fix #217
1 parent e696074 commit cb44eed

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/runtime/components/css.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
7777
async setup(props) {
7878
const nuxt = useNuxtApp()
7979
const options = useAppConfig().icon as NuxtIconRuntimeOptions
80-
const cssClass = computed(() => options.cssSelectorPrefix + props.name)
80+
const cssClass = computed(() => props.name ? options.cssSelectorPrefix + props.name : '')
8181

8282
function getIcon(name: string) {
83-
const registry = _getIcon(name)
84-
if (registry)
85-
return registry
83+
if (!name)
84+
return
85+
const icon = _getIcon(name)
86+
if (icon)
87+
return icon
8688
const payload = nuxt.payload.data[name]
8789
if (payload) {
8890
addIcon(name, payload)
@@ -173,7 +175,7 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
173175
})
174176
}
175177
// Dedupe CSS
176-
if (!ssrCSS.has(props.name)) {
178+
if (props.name && !ssrCSS.has(props.name)) {
177179
const css = getCSS(icon, false)
178180
ssrCSS.set(props.name, css)
179181
}

src/runtime/components/shared.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { computed } from 'vue'
22
import { loadIcons, getIcon as _getIcon } from '@iconify/vue'
3+
import type { IconifyIcon } from '@iconify/types'
34
import type { NuxtIconRuntimeOptions } from '../../types'
45
import { useAppConfig } from '#imports'
56
import { init } from '#build/nuxt-icon-client-bundle'
67

7-
export async function loadIcon(name: string) {
8+
export async function loadIcon(name: string): Promise<Required<IconifyIcon> | null> {
9+
if (!name)
10+
return null
811
init()
912
await new Promise(resolve => loadIcons([name], () => resolve(true)))
1013
.catch(() => null)

src/runtime/components/svg.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@ export const NuxtIconSvg = /* @__PURE__ */ defineComponent({
2424

2525
const storeKey = 'i-' + name.value
2626

27-
// On server side, we fetch the icon data and store it in the payload
28-
if (import.meta.server) {
29-
useAsyncData(
30-
storeKey,
31-
() => loadIcon(name.value),
32-
{ deep: false },
33-
)
34-
}
27+
if (name.value) {
28+
// On server side, we fetch the icon data and store it in the payload
29+
if (import.meta.server) {
30+
useAsyncData(
31+
storeKey,
32+
() => loadIcon(name.value),
33+
{ deep: false },
34+
)
35+
}
3536

36-
// On client side, we feed Iconify we the data we have from server side to avoid hydration mismatch
37-
if (import.meta.client) {
38-
const payload = nuxt.payload.data[storeKey]
39-
if (payload) {
40-
addIcon(name.value, payload)
37+
// On client side, we feed Iconify we the data we have from server side to avoid hydration mismatch
38+
if (import.meta.client) {
39+
const payload = nuxt.payload.data[storeKey]
40+
if (payload) {
41+
addIcon(name.value, payload)
42+
}
4143
}
4244
}
4345

0 commit comments

Comments
 (0)