问题原文
原帖链接 · 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"
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
