31

在 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 中收到相同的消息。如何正确缓存它?

4

2 回答 2

87

这是为我做的工作,因为谷歌页面速度不再要求修复它。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 application/font-woff2  "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 year"
于 2015-01-26T10:07:38.553 回答
3

在Seb 的 IT 博客的帮助下,这对我有用:

<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>
于 2021-07-01T04:51:28.393 回答