User Device-Based Caching

user device based caching

It could be required to cache an asset based on the client device. For instance, if you want to cache and deliver a different layout to your mobile users than your desktop users you will need to take advantage of device-based caching. On the other hand, if you are taking advantage of responsive web design you don’t need to worry about device-based caching as all devices will access the same assets, letting CSS perform the sizing adjustments.

In the event that you do require device-based caching, this guide will show you how KeyCDN’s user device-based caching feature works and how you can start using it.

How Does User Device-Based Caching Work?

The device-based caching feature will allow you to cache your assets for each specific device. The device type is determined by the User-Agent header sent from the client. Three different device categories exist for this caching approach:

  • desktop
  • mobile
  • tablet

The KeyCDN edge servers send the X-Device header when they fetch the content from the origin server:

X-Device: desktop

The default value is always desktop, however, will change based on the device type used by the client. The Nginx rules implemented to enable user device-based caching have different regular expression patterns for “mobile” and “tablet” to detect the device used by the client.

map $http_user_agent $ua_device {
    default "desktop";
    "~*(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|mobile.+firefox|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ ce|xda|xiino/i" "mobile";
    "~*android|ipad|playbook|silk/i" "tablet";

Purging User Device-Based Assets

If user device-based caching is enable in your KeyCDN zone and you want to purge a particular asset it is required to append the relevant device tag at the end of the URL, for instance:

The example above will purge only the mobile version of that asset. However, if you wanted to completely remove the asset from cache, regardless of device type, you would purge using the following.


User device-based caching can be very useful for those who deliver different assets to users based on the devices they are using. This feature is disabled by default for all new zones however if you want to enable it, simply navigate to your zone’s advanced features section in the KeyCDN dashboard, enable the user device-based caching option, and wait until your zone is redeployed.