【发布时间】:2020-01-06 06:57:46
【问题描述】:
在当今支离破碎的网络图标环境中(令人惊讶的是).svg 图标尚未获得广泛支持,人们可能会想在头部添加大量元素指向任意数量的元素
faviconsiconsapple-touch-iconssafari-pinned-tabsmstiles
等等
任何页面上的网站图标、图标、触摸图标、磁贴等的综合列表都会非常庞大。
这会影响页面初始化和加载时间,在某些情况下会非常显着。
金发姑娘的解决方案是能够根据需要添加尽可能多的图标 <link> 引用,而不会影响页面加载性能。
那么...异步加载这些元素是一种合法的方法吗?
例如。
const favIcon = {'rel' : 'icon', 'href' : 'favicon.ico'};
const appleTouchIcon = {'rel' : 'apple-touch-icon', 'href' : 'apple-touch-icon.png'};
const myIcons = [favIcon, appleTouchIcon];
for (let i = 0; i < myIcons.length; i++) {
let myIcon = document.createElement('link');
myIcon.rel = myIcons[i].rel;
myIcon.href = myIcons[i].href;
document.head.appendChild(myIcon);
}
或者在DOMContentLoaded 上异步加载这些<link> 元素意味着各种用户代理会错过他们正在寻找的图标?
【问题讨论】:
-
仅仅因为它们被定义并不意味着它们被加载。例如,
apple-touch-icon文件只有在您将页面保存到主屏幕时才会被使用。
标签: javascript performance icons favicon apple-touch-icon