如何利用.woff字体的浏览器缓存?

时间:2014-10-24 17:27:11

标签: browser-cache woff

在PageSpeed Insights中,我一直看到消息来利用我正在使用的特定图标集/字体的浏览器缓存:iconFont.woff(2天)

我将我的.htaccess设置为:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType font/ttf "access 1 week"
ExpiresByType font/woff "access 1 week"
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month" 
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

尽管如此,我仍然在PageSpeed Insights中收到相同的消息。如何正确缓存?

2 个答案:

答案 0 :(得分:67)

这是我的工作,因为Google页面速​​度不再要求修复它。 AddType是必不可少的。

# Fonts
# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot 
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff
AddType image/svg+xml .svg

# Compress compressible fonts
# only uncomment if you dont have compression turned on already. Otherwise it will cause all other filestypes not to get compressed
# AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-opentype image/svg+xml

ExpiresActive on

# Add a far future Expires header for fonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/x-font-opentype "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

答案 1 :(得分:0)

Seb's IT blog 的帮助下,这对我有用:

<IfModule mod_expires.c>
  # Activate mod
  ExpiresActive on

  # Declare fonts content-type
  AddType application/x-font-woff2 .woff2

  # Set cache duration
  ExpiresByType application/x-font-woff2  "access plus 1 month"

  # Append "public" to header "Cache-Control"
  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>
</IfModule>