INTEGRITY Cloudflare Docs

Vary for images

The Vary HTTP response header tells Cloudflare that an origin can serve different versions of the same resource depending on the request headers. For images, this allows your origin to serve modern formats like WebP or AVIF to browsers that support them, while continuing to serve JPEG or PNG to others.

When Cloudflare receives a response with image variants, it caches each variant separately. Subsequent requests from browsers with the same image format preferences are served directly from cache without contacting your origin.

Vary for Images works by parsing the Accept header in each request to determine which image format the browser supports, then serving the matching cached variant.

Vary for images is available for Pro, Business, and Enterprise customers.

Availability

Free Pro Business Enterprise
Availability No Yes Yes Yes

File extensions

You can use vary for images on the file extensions below if the origin server sends the Vary: Accept response header. If the origin server sends Vary: Accept but does not serve the set variant, the response is not cached and displays BYPASS in the cache status in the response header. Additionally, the list of variant types the origin serves for each extension must be configured so that Cloudflare decides which variant to serve without contacting the origin server.

File extensions enabled for varying

Enable vary for images

Vary for Images is enabled through Cloudflare's API by creating a variants rule. In the examples below, learn how to serve JPEG, WebP, and AVIF variants for .jpeg and .jpg extensions.

Create a variants rule

Required API token permissions

At least one of the following token permissions is required:
Change variants setting
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/cache/variants" \
	--request PATCH \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"value": {
				"jpeg": [
						"image/webp",
						"image/avif"
				],
				"jpg": [
						"image/webp",
						"image/avif"
				]
		}
	}'

Modify to only allow WebP variants

Required API token permissions

At least one of the following token permissions is required:
Change variants setting
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/cache/variants" \
	--request PATCH \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"value": {
				"jpeg": [
						"image/webp"
				],
				"jpg": [
						"image/webp"
				]
		}
	}'

Delete the rule

Required API token permissions

At least one of the following token permissions is required:
Delete variants setting
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/cache/variants" \
	--request DELETE \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"

Get the rule

Required API token permissions

At least one of the following token permissions is required:
Get variants setting
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/cache/variants" \
	--request GET \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"

To learn more about purging varied images, refer to Purge varied images.

Limitations