Set a sitewide cache policy with SEO constraints: TTLs, etags, and stale-while-revalidate.
- User
Set a sitewide cache policy with SEO constraints: TTLs, etags, and stale-while-revalidate.
Assistant
Set a sitewide cache policy with SEO constraints: TTLs, etags, and stale-while-revalidate.
The user wants a sitewide cache policy configuration that balances performance (caching) with SEO constraints (Googlebot crawl budget, Core Web Vitals). The specific technologies mentioned are:
The user hasn't specified the server technology (Nginx, Apache, Varnish, Cloudflare/CDN, or a specific backend like Node/Python). I should provide a solution that covers the conceptual strategy (the "Policy") and then provide concrete implementation examples for the most common scenarios (Nginx/Apache/CDN).
Key SEO Constraints to consider:
max-age, it saves crawl budget. This is good.stale-while-revalidate is excellent here because it serves instant content while updating in the background, preventing "stall" feelings.Structuring the response:
Cache-Control, ETag, Last-Modified.noindex tags interact with caching and ensure Googlebot isn't blocked.Drafting the Policy (Mental Sandbox):
HTML: We want Google to see changes fast, but we want users to load fast.
Cache-Control: public, max-age=600, stale-while-revalidate=86400, must-revalidate.max-age=600 (10 mins) ensures a user doesn't wait long for a fresh check, but SWR ensures the next user gets the old version instantly while the server refreshes. ETag helps save bandwidth if nothing changed.Static Assets (Images/CSS/JS):
Cache-Control: public, max-age=31536000, immutable.CDN/Edge logic:
Cache-Control: private, no-store).Refining the SEO Constraints: Google acts as a "user" but consumes resources differently.
max-age=604800), and I change the meta title, Google won't see it for a week unless I purge or use stale-while-revalidate effectively.max-age (freshness window) + higher stale-while-revalidate (performance window) + ETags (bandwidth saving).Finalizing the Output Structure:
Let's write the Nginx config mentally to ensure it works.
etag on; (Default usually, but good to state).
add_header Cache-Control ...
Need to distinguish file types using map or location blocks. map is cleaner for a "sitewide" policy.
Self-Correction during drafting: Don't just dump code. Explain why specific numbers are chosen (e.g., why 1 year for images vs 10 mins for HTML).
Double Check Stale-While-Revalidate: Does Googlebot respect SWR? Yes, but it's subtle. SWR is mostly for the browser/CDN. For SEO, the crucial part is the max-age determining how often the crawler checks back. Actually, Last-Modified and ETag (triggering 304) are the primary way to handle crawl budget efficiently.
Let's proceed with generating the response. A sitewide cache policy optimized for SEO requires a delicate balance: you need high cache hit rates (low TTFB) for ranking signals, while ensuring Googlebot sees content updates quickly to prevent indexation lag.
Here is a comprehensive cache policy configuration designed to balance Performance (Core Web Vitals) with SEO (Crawl Efficiency & Freshness).
To satisfy SEO constraints, we must treat HTML and Assets differently:
This is the logic you should implement at your server or CDN level (Nginx, Apache, Cloudflare, Varnish).
text/html, application/json)max-age): Short (e.g., 0 to 10 minutes).stale-while-revalidate): Long (e.g., 1 day to 1 week).Accept-Encoding (Critical to prevent serving Gzip to Brotli clients).Logic:
"Check with the server every 10 minutes. If the server is down or slow, serve the stale version for up to 1 day while fetching a new copy in the background."
Header Output:
Cache-Control: public, max-age=600, stale-while-revalidate=86400, must-revalidate
ETag: "5d8c72a5-3f2"
Vary: Accept-Encoding
robots.txt. Google needs to render them for CWV.Header Output:
Cache-Control: public, max-age=31536000, immutable
ETag: "unique-file-hash"
Here is how to set this up in common environments.
Add this to your server or location block.
server {
# ... other config ...
# 1. Enable ETags (Usually default, but ensure it's on)
etag on;
# 2. HTML Policy (SEO Optimized)
# Match HTML and JSON (if API drives UI)
location ~* \.(?:html|json)$ {
# Disable access logs for 304 responses to save disk
log_not_found off;
# Policy: 10 min fresh, 1 day stale allowed
add_header Cache-Control "public, max-age=600, stale-while-revalidate=86400, must-revalidate";
# Ensure we don't serve wrong compression to bots
add_header Vary "Accept-Encoding";
try_files $uri $uri/ /index.html;
}
# 3. Static Assets Policy (Performance Optimized)
location ~* \.(?:css|js|jpg|jpeg|gif|png|ico|woff2|svg|webp)$ {
# Policy: 1 Year + Immutable (Content changes require filename change)
add_header Cache-Control "public, max-age=31536000, immutable";
add_header Vary "Accept-Encoding";
access_log off;
log_not_found off;
expires 1y;
}
}
Add this to your .htaccess file in the root directory.
<IfModule mod_headers.c>
# 1. Enable ETags (FileETag None + All usually works best)
# If on Apache 2.4+, 'FileETag MTime Size' is standard.
# Ensure mod_deflate is setting Vary correctly
Header append Vary: Accept-Encoding
# 2. HTML Policy (Short TTL, Long SWR)
<FilesMatch "\.(html|json)$">
Header set Cache-Control "public, max-age=600, stale-while-revalidate=86400, must-revalidate"
</FilesMatch>
# 3. Static Assets Policy (Long TTL)
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|webp|js|css|woff2|ttf|svg)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
</IfModule>
If using a CDN, do not rely solely on server headers. Set page rules:
When implementing this, keep these Google-specific constraints in mind:
1. Googlebot's Crawl Budget & 304s
ETags or Last-Modified headers, Googlebot will send a If-None-Match request.2. Why immutable is SEO-safe for CSS/JS
immutable only on assets that use "cache busting" (e.g., style.a1b2c3.css).style.a1b2c4.css.3. The Stale-While-Revalidate Risk
max-age is 600 seconds, a user might see the "In Stock" status for up to 10 minutes.4. Vary: User-Agent (Avoid This)
Vary: User-Agent.Vary: Accept-Encoding only.| Resource | Cache-Control Strategy | SEO Impact |
|---|---|---|
| HTML | max-age=600, stale-while-revalidate=86400 | High TTFB (cached), Low Latency (SWR). Google revalidates often via ETags/304. |
| CSS/JS | max-age=31536000, immutable | Perfect LCP. Requires filename versioning. |
| Images | max-age=31536000 | Best CLS. High cache hit rate. |