css - 字体会导致性能问题

标签 css font-face

当我的网站加载时,在浏览器显示使用字体呈现的文本之前会卡住几秒钟(立即显示 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');
}

最佳答案

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

这里很helpful overview来自表演大师 Steve Sounders

其他信息和资源

假设您使用 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 control 的速度提示, 和 compression .

关于css - 字体会导致性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8858159/

相关文章:

javascript - float 在屏幕上的链接上的 qtip2 工具提示

javascript - 如何向表格添加新行并更改其颜色?

python - 使用 beautifulSoup 抓取 CSS 信息

css - 并非所有字体都应用了 CSS3

html - 在 HTML 页面中添加自定义字体

css - @fontface 没有正确安装

css - 当元素为 CSS3 动画时,Safari 应用字体效果/处理

CSS 选择器 : parent and child element

javascript - 像 fb messenger 一样长按增加图标/图像的大小

css - 无法理解防弹@font-face CSS 规则