Mastering the Vary Header: Smarter Caching for Personalized Content

Mastering the Vary Header: Smarter Caching for Personalized Content
Photo by Jakub Żerdzicki / Unsplash

When building fast and modern websites, caching is one of your best tools—but it can also become a trap. If you're not careful, your CDN or browser might serve the wrong content to the wrong user. That's where the Vary header comes in. It's not just a technical detail—it's a powerful way to serve personalized, localized, and optimized content without sacrificing performance.

Let’s break it down.


📌 What Is the Vary Header?

The Vary header is an HTTP response header that tells caches to store different versions of a resource depending on the request headers.

Think of it like this: if your server behaves differently based on the request’s Accept-Language or X-Country, then your cache should too. Otherwise, someone in Jakarta might end up seeing a German version of your homepage.

Vary: Accept-Language

This simple line means:

“Cache this page differently depending on what language the user’s browser prefers.”

🎯 Why You Should Care

Without the Vary header, caches might assume that every user should see the same version of your content. This leads to:

  • Incorrect languages being shown
  • Wrong region-specific data (pricing, offers, layout)
  • Inconsistent behavior for mobile vs desktop users

By using Vary, you’re telling your CDN:

“Hold multiple versions of this response, depending on some specific request headers.”

That small change can massively improve user experience—without adding server overhead.


🚀 Real-World Use Cases

Here are some powerful examples of where Vary makes a real difference:

Use Case Vary On Header Why?
Localized content Accept-Language Serve pages in the user’s language
Geo-specific offers X-Vercel-IP-Country Show prices, promos, or layout based on country
Device-specific layout User-Agent (be careful) Different experience for mobile vs desktop
Compression strategies Accept-Encoding Use gzip, brotli, or none depending on browser support

For example, on Vercel:

Vary: X-Vercel-IP-Country
Cache-Control: s-maxage=60

This tells the edge network to cache one version per country for 60 seconds, which means your users in the US, Indonesia, or Germany all get fast, localized content with no recomputation on your backend.


⚠️ Gotchas & Considerations

Using Vary sounds great—and it is—but there are some things to watch for:

1. Cache Fragmentation

The more headers you vary on, the more versions of your content get cached. This can explode your cache size and reduce effectiveness if overused.

2. Poor CDN Support (in some cases)

Not all CDNs respect Vary correctly. Luckily, Vercel, Cloudflare, and most modern edge networks handle it well. But if you use legacy proxies or setups, test it carefully.

3. User-Agent Vary Is a Trap

Using Vary: User-Agent is tempting for device targeting, but it's very risky. User-Agent strings vary wildly, and this can destroy your cache. If you must do it, normalize the UA string first.

4. Order Doesn’t Matter—But Be Consistent

HTTP headers are case-insensitive, and the order of Vary values doesn’t technically matter, but it’s good practice to be clean and predictable:

Vary: Accept-Language, X-Custom-Header

✅ Best Practices

  • Only use Vary when content actually changes based on a header.
  • Test your cache behavior—especially with CDNs and edge networks.
  • Avoid stacking too many headers in Vary. Think of it as a scalpel, not a hammer.
  • Use it together with proper cache control (Cache-Control, s-maxage, etc.) for maximum effect.

💡 Finally

The Vary header is one of those underrated HTTP tools that can transform your caching strategy from basic to intelligent and personalized. It’s about delivering the right content to the right user, at the right speed—without duplicating work on your servers.

If you're building a localized, multilingual, or multi-device experience—don’t skip the Vary header. It’s not just a performance tweak—it’s a feature enabler.

Support Us