Score:0

Create cache context for anonymous users based on ip range

mx flag
awm

I have a form within a custom block that is displayed on the front page of a Drupal 9 site on Acquia Cloud. I want to be able to have different displays for users coming from certain ip ranges. Is this possible to do while leaving all Drupal caching such as dynamic page cache and internal page cache?

If it is possible, how do I create cache contexts for this custom block/form such that Drupal knows to cache different displays based on that context? any examples would be great.

I know I can possibly accomplish this on the client side or using ajax but I would like to avoid that.

4uk4 avatar
cn flag
There is already an ip based context. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Cache%21Context%21IpCacheContext.php/class/IpCacheContext/9.2.x. But if you have only a few ip ranges you could implement a custom cache context returning only those regions and not each single ip and so save a lot of cache space. BTW you have to uninstall the Internal Page Cache, it doesn't work with cache contexts.
awm avatar
mx flag
awm
thanks - i'll give it a try but I'm considering a different approach since dealing with cache can have undesired consequences.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.