Skip to content

Commit d582e62

Browse files
authored
fix(icon): add better warning when loading icons (#1297)
1 parent 625d54f commit d582e62

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/components/icon/utils.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const getUrl = (i: Icon) => {
6464

6565
url = getName(i.name, i.icon, i.mode, i.ios, i.md);
6666
if (url) {
67-
return getNamedUrl(url);
67+
return getNamedUrl(url, i);
6868
}
6969

7070
if (i.icon) {
@@ -83,12 +83,23 @@ export const getUrl = (i: Icon) => {
8383
};
8484

8585

86-
const getNamedUrl = (iconName: string) => {
86+
const getNamedUrl = (iconName: string, iconEl: Icon) => {
8787
const url = getIconMap().get(iconName);
8888
if (url) {
8989
return url;
9090
}
91-
return getAssetPath(`svg/${iconName}.svg`);
91+
try {
92+
return getAssetPath(`svg/${iconName}.svg`);
93+
} catch(e) {
94+
/**
95+
* In the custom elements build version of ionicons, referencing an icon
96+
* by name will throw an invalid URL error because the asset path is not defined.
97+
* This catches that error and logs something that is more developer-friendly.
98+
* We also include a reference to the ion-icon element so developers can
99+
* figure out which instance of ion-icon needs to be updated.
100+
*/
101+
console.warn(`[Ionicons Warning]: Could not load icon with name "${iconName}". Ensure that the icon is registered using addIcons or that the icon SVG data is passed directly to the icon component.`, iconEl);
102+
}
92103
};
93104

94105

0 commit comments

Comments
 (0)