为什么在Firefox上Google字体无法缓存?

6
我正在尝试在我的网站上使用嵌入式Google字体,并在每次有人访问网站时包含从Google字体API下载字体的链接,但我在Firefox上遇到了问题,因为它似乎在每次刷新或点击新链接时都在尝试下载字体。
在所有其他浏览器上,它只下载一次并缓存字体,就像缓存其他内容一样。
Google字体API样式表的链接如下:
<link href='http://fonts.googleapis.com/css?family=Droid+Sans&subset=latin' rel='stylesheet' type='text/css'>
1个回答

5
我注意到了同样的行为;使用JavaScript加载字体似乎可以解决这个问题。只需在您的情况下将“Ubuntu”替换为“Droid”,并在<head>标签后插入以下代码块即可:
<script type="text/javascript">
  WebFontConfig = {
    google: { families: [ 'Ubuntu' ] }
  };
  (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
        '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
  })();
</script>

更多信息可以在Google开发者的字体网站上找到。


不错,实际上,我在Chrome浏览器中也遇到了这个问题,用懒加载修复了它。 - Augustin Riedinger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接