Reduce a DNS+TCP by Caching Gravatar at CDN

The default commenting system of WordPress uses Gravatar services to display the profile picture of the comment author.

gravatar cache

There are three advantages of Caching Gravatar images and serving from CDN

  1. HTTP/2 allows parallel downloading, so it is better to reduce the unnecessary DNS lookup and TCP timing that goes in the extra domain (secure.gravatar.com).
  2. By default, it caches images only for 300 seconds in a browser that we can increase.
  3. Concerning GDPR, we can limit the collection of log information by sending the BunnyCDN IP address to the Gravatar server instead of the actual visitor’s IP. 👏

Prerequisite

  • A pull zone with a custom hostname – www.example.com for HTML Caching. 🔥
  • Or, a Pull Zone with traditional cdn.example.com static content caching

To achieve this, first we need to create few edge rules then make search replace in Document.

Step 1. Set custom Request Header

  • Create an Edge rule
  • Action: Set Request Header
  • Header name: host
  • Header value: secure.gravatar.com
  • Condition Matching: Match Any
  • ConditionsIF Request URLMatch Anyhttps://www.example.com/avatar/*

Step 2. Override origin to secure.gravatar.com

Step 3. Enforce CDN Caching

  • Create another Edge rule
  • Action: Override Cache Time
  • Cache time in seconds: 31536000
  • Condition Matching: Match Any
  • ConditionsIf Request URLMatch Any: https://www.example.com/avatar/*

Step 4. Enable Browser Caching for Gravatar

  • Create another Edge rule
  • Action: Set Response Header
  • Header name: Cache-Control
  • Header Value: max-age=31536000
  • Condition Matching: Match Any
  • ConditionsIf Request URLMatch Any: https://www.example.com/avatar/*

Step 5. Serve Images from CDN hostname

  • Take a backup before proceeding.
  • Install and Activate ‘Real Time Find and Replace‘ WordPress Plugin to make on-the-fly changes in the HTML document.
  • Go to Tools and menu setup this plugin.
  • Findhttps://secure.gravatar.com
  • Replace: https://www.example.com

That’s all. Now all your Gravatar images will appear from www.example.com address.

Any question? Please ask at Gulshan Forum.

Leave a Comment