大网数据广告活动
网站运营与故障问答

How to Leverage Browser Caching for Fonts in WordPress

· 52CE 我爱测

I am using WordPress along with Autoptimize and Cloudflare, and using Locally hosted fonts. I can't leverage browser caching on my site. I tried using Cloudflare Page rules to Leverage 1 year browser caching on all resources. It …

问题原文

原帖链接 · Aditya Agarwal · CC BY-SA 4.0

I am using WordPress along with Autoptimize and Cloudflare, and using Locally hosted fonts. I can't leverage browser caching on my site.

I tried using Cloudflare Page rules to Leverage 1 year browser caching on all resources.

It worked, and most of my site was getting cached EXCEPT FONTS.

I know fonts are heavy and can have negative impact on performance.

I tried the following code in htaccess file in my server root.

# 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

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault "access plus 2 days"
    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 year"
</IfModule>
# END Expire headers

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
    <filesMatch "\.(ico|jpe?g|png|gif|swf|mp4|mov|css|js|json|woff|woff2)$">
        Header add Cache-Control "public"
    </filesMatch>
    <filesMatch "\.(x?html?|php)$">
        Header add Cache-Control "private, must-revalidate"
    </filesMatch>
</ifModule>

But it didn't work.

As far as I know the below above code should have done the trick to cache the Fonts and JSON.

Please help me figure out the correct way to implement it.

Warnings about fonts not being cached:

Leverage browser caching of static assets: 91/100 Learn More WARNING - (2.0 days) - https://milyin.com/superpwa-manifest.json

Leverage browser caching of static assets: 91/100 Learn More WARNING - (2.0 days) - https://milyin.com/wp-content/uploads/omgf/neve-google-font-poppins/poppins-normal-700.woff2

WARNING - (2.0 days) - https://milyin.com/wp-content/uploads/omgf/neve-google-font-poppins/poppins-normal-400.woff2

WARNING - (2.0 days) - https://milyin.com/wp-content/uploads/omgf/neve-google-font-poppins/poppins-normal-500.woff2

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

Your mime types don't match your ExpireByType directives:

$ curl --head https://milyin.com/wp-content/uploads/omgf/neve-google-font-poppins/poppins-normal-700.woff2
...
expires: Thu, 09 Sep 2021 13:24:15 GMT
...
content-type: font/woff2

So you should change your .htaccess. Instead of, or in addition to

ExpiresByType application/font-woff2  "access plus 1 year"

you should have

ExpiresByType font/woff2  "access plus 1 year"

Here are the correct mime types for fonts:

$ grep font /etc/mime.types 
application/font-sfnt               otf ttf
application/font-tdpfr              pfr
application/font-woff               woff
application/vnd.font-fontforge-sfd                      sfd
application/vnd.ms-fontobject                           eot
application/x-font              pfa pfb gsf
application/x-font-pcf              pcf pcf.Z
font/collection                 ttc
font/otf                    ttf otf
font/sfnt                   ttf otf
font/ttf                    ttf otf
font/woff                   woff
font/woff2                  woff2

You could use a wildcard rule like:

ExpiresByType font/* "access plus 1 year"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-09-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。