【问题标题】:font face causes performance issues字体导致性能问题
【发布时间】:2012-02-10 02:30:19
【问题描述】:

当我的网站加载时,在浏览器显示使用字体呈现的文本之前会冻结几秒钟(使用 arial 字体的文本会立即显示)。

我收到了用户的投诉,他们的卡顿时间长达 10 秒。

对此我能做些什么?

这是我插入字体的方式:

@font-face{
    font-family:'GillSans';
    src:url('../fonts/GIL_____.eot');
    src:url('../fonts/GIL_____.eot?#iefix') format('embedded-opentype'),
        url('../fonts/GIL_____.woff') format('woff'),
        url('../fonts/GIL_____.ttf') format('truetype'),
        url('../fonts/GIL_____.svg#GillSans') format('svg');
}
@font-face{
    font-family:'GillSansB';
    src:url('../fonts/GILB____.eot');
    src:url('../fonts/GILB____.eot?#iefix') format('embedded-opentype'),
        url('../fonts/GILB____.woff') format('woff'),
        url('../fonts/GILB____.ttf') format('truetype'),
        url('../fonts/GILB____.svg#GillSansB') format('svg');
}
@font-face{
    font-family:'GillSansBI';
    src:url('../fonts/GILBI___.eot');
    src:url('../fonts/GILBI___.eot?#iefix') format('embedded-opentype'),
        url('../fonts/GILBI___.woff') format('woff'),
        url('../fonts/GILBI___.ttf') format('truetype'),
        url('../fonts/GILBI___.svg#GillSansBI') format('svg');
}
@font-face{
    font-family:'GillSansI';
    src:url('../fonts/GILI____.eot');
    src:url('../fonts/GILI____.eot?#iefix') format('embedded-opentype'),
        url('../fonts/GILI____.woff') format('woff'),
        url('../fonts/GILI____.ttf') format('truetype'),
        url('../fonts/GILI____.svg#GillSansI') format('svg');
}

【问题讨论】:

  • 字体大小是多少?您描述的浏览器有哪些问题?
  • 其实延迟最多的是chrome

标签: css font-face


【解决方案1】:

尝试压缩和缓存您的字体。如果您使用 Apache,您可以使用 .htaccess 进行配置

这是来自表演大师 Steve Sounders 的非常 helpful overview

其他信息和资源

假设您使用 Apache 并且启用了模块 mod_expiresmod_deflate,您可以将以下规则添加到您的 .htaccess 中

<IfModule mod_expires.c>
  Header set cache-control: public
  ExpiresActive on

  ExpiresByType font/ttf      "access plus 1 month"
  ExpiresByType font/woff     "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
</IfModule>

<IfModule mod_deflate.c>
  <FilesMatch "\.(ttf|otf|eot|svg)$" >
    SetOutputFilter DEFLATE
  </FilesMatch>
</IfModule>

将以上内容添加到.htaccess后,观察相关头域进行验证。

如果您有兴趣了解更多信息,请查看cache controlcompression 的速度提示。

【讨论】:

  • "Typekit 显然在做某事——自定义字体的使用在一年内增加了两倍。出于性能原因,我警告不要使用 @font-face,但是性能并不是最重要的。(喘气!)自定义字体显然具有对网站所有者有吸引力的审美优势。" Steve Souders, Nov 2011
  • 非常有趣的文章。我不明白的是如何缓存字体文件?
  • 我添加了其他信息。祝你好运!
猜你喜欢
  • 2019-05-04
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 2013-01-07
相关资源
最近更新 更多