添加Expires Headers - GTmetrix检测:5个静态组件无远期过期日期
Alright, let's tackle that Expires Headers warning from GTmetrix. The 5 static components flagged are all external assets—Google Fonts stylesheets and a WordPress.com tracking pixel—and here's how to resolve each case:
First, let's recap the resources GTmetrix called out:
https://fonts.googleapis.com/css?family=Poppins%3A300%2Cregular%2C500%2C600%2C700%2Clatin-ext%2Clatin%2Cdevanagari&ver=1.0.0https://fonts.googleapis.com/css?family=Poppins%3A700%2C300&ver=1581165984https://fonts.googleapis.com/css?family=Poppins:700%2C300%2C400%7CRoboto:400%2C900https://fonts.googleapis.com/css?family=Montserrat%3Aregular%2C700&ver=6.0.5https://pixel.wp.com/g.gif?v=ext&j=1%3A8.1.1&blog=167300497&post=1633&tz=0&srv=alshuwaifatmarbles.com&host=alshuwaifatmarbles.com&ref=&fcp=2042&rand=0.1473167431208402
Since these are hosted outside your server, you can't directly edit their configs to add Expires Headers. Here are your actionable fixes:
1. Resolve Google Fonts Stylesheets
Google Fonts doesn't set long-enough cache headers on their CSS files, so we have two reliable workarounds:
Option A: Host Fonts Locally (Most Control)
By moving the fonts to your own server, you get full say over cache rules:
- Visit each Google Fonts CSS link in your browser, copy the
@font-facerules, and download the corresponding.woff2/.ttffont files. - Upload the font files to a folder in your WordPress theme (e.g.,
wp-content/themes/your-theme/fonts/). - Replace the existing Google Fonts
<link>tags in your theme with the local@font-facerules in your custom CSS. - Add cache rules to your server config to set far-future Expires Headers:
- For Apache (add to
.htaccess):<IfModule mod_expires.c> ExpiresActive On # Cache fonts for 1 year ExpiresByType font/woff "access plus 1 year" ExpiresByType font/woff2 "access plus 1 year" ExpiresByType font/ttf "access plus 1 year" </IfModule> - For Nginx (add to your server block):
location ~* \.(woff|woff2|ttf)$ { expires 1y; add_header Cache-Control "public, immutable"; }
- For Apache (add to
Option B: Cache via CDN
If you use a CDN like Cloudflare, create a rule to cache these Google Fonts resources. Most CDNs let you set a 1-year cache duration for third-party assets, which will add proper Expires Headers when the CDN serves the content to your visitors.
2. Handle the WordPress.com Tracking Pixel (pixel.wp.com/g.gif)
This is a third-party tracking asset, so you have two choices:
- Disable it: If you don't need WordPress.com's stats, turn off this feature via your WordPress settings or use a plugin to remove the tracking code entirely. This eliminates the resource from loading at all.
- Proxy it through your server: If you must keep it, set up a server proxy to fetch the pixel through your domain. This lets you add Expires Headers to the proxied request. For example, use Apache's
mod_proxyto map a local path to the pixel URL, then add cache rules for that path. Note that this requires server admin access and basic config knowledge.
内容的提问来源于stack exchange,提问作者Daud Khan

