What does it do?
When a user requests an asset such as an image, CSS file, or HTML document, the request goes through the StackPath CDN edge network. If the StackPath CDN server already has the asset, it is served from cache instead of requesting it directly from the origin server. If the asset isn't cached on the CDN then the CDN pulls it from the origin server and stores it in its cache.
StackPath creates a unique cache key for each resource it stores in cache. A cache key is an "ID" for each resource that StackPath stores in cache. Keys are unique and are constructed from several variables such as protocol, like HTTP or HTTPS, and URL.
The "Dynamic caching by header" feature allows you to extend the variables used to create a cache key by adding a specific request header.
When and how should I use it?
It is possible to create specific cache keys based on request headers to deliver specific content.
For example, if your origin provides different images (same image URL/name) for mobile and desktop users, then you can create a specific cache key for those mobile users by adding a specific header. For example, using
x-mobile:true creates a new cache key for mobile asset requests, where the protocol and URL match, and the +
x-mobile:true header is present. Desktop assets will still be created from the original protocol and URL.
To implement the above functionality, create the following EdgeRule:
If Header Matches User-Agent: *Android*
Then Add Header to CDN X-mobile true
"iPhone" can be used in place of "Android" to create the "x-mobile: iPhone" header instead. It is also possible to use "Regex Matches" with a value of "/.*iPhone.*|.*Android.*/" for more versatility.
Now, navigate to the CDN section on the left menu bar, and enable Dynamic Caching By Header. Add "x-mobile" as an additional cache key. Once that's in place all requests from mobile users are served from StackPath CDN's cache.
If any questions arise or any assistance can be further provided, feel free to drop us a line at email@example.com or open up a chat with our 24/7 live support, we'll be happy to help you out.