---
name: "Vary"
area: "API"
kind: "HTTP header"
parent: "up.protocol module"
url: "https://unpoly.com/api/Vary"
---
<nav aria-label="Unpoly docs">[All docs](https://unpoly.com/index.md) · [up.protocol module](https://unpoly.com/api/up.protocol.md)</nav>

# Vary

Request headers that influenced a response should be listed in a `Vary` response header.

This tells Unpoly to [partition its cache](https://unpoly.com/learn/optimizing-responses.md#vary) for that URL so that each request header value gets a separate cache entry.

You can set a `Vary` header manually from your server-side code. You may also be using a library like [unpoly-rails](https://github.com/unpoly/unpoly-rails) that sets the `Vary` header automatically.

> #### Tip
>
> Server-side Unpoly apps may inspect [request headers](https://unpoly.com/api/up.protocol.md) to [customize or shorten responses](https://unpoly.com/learn/optimizing-responses.md), e.g. by omitting content that isn't [targeted](https://unpoly.com/learn/targeting-fragments.md). When a response is optimized in that fashion, a `Vary` header should be sent.

## Example {#example}

The user makes a request to `/sitemap` in order to update a fragment `.menu`. Unpoly makes a request like this:

```http
GET /sitemap HTTP/1.1
X-Up-Target: .menu
```

The server may choose to [optimize its response](https://unpoly.com/learn/optimizing-responses.md) by only rendering the HTML for the `.menu` fragment. It responds with the HTTP seen below. Note that it includes a `Vary` header indicating that the [`X-Up-Target`](https://unpoly.com/api/X-Up-Target.md) header has influenced the response body:

```http
Vary: X-Up-Target

<div class="menu">...</div>
```

After observing the `Vary: X-Up-Target` header, Unpoly will partition cache entries to `/sitemap` by [`X-Up-Target`](https://unpoly.com/api/X-Up-Target.md) value. That means a request targeting `.menu` is no longer a cache hit for a request targeting a different selector.

See [How cache entries are matched](https://unpoly.com/learn/optimizing-responses.md#cache-matching) for more examples.

## Resources {#resources}

- [RFC 9110: Vary](https://httpwg.org/specs/rfc9110.html#field.vary)
- [MDN: Vary](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary)
- [Key CDN: Vary](https://www.keycdn.com/support/vary-header)

