Block caching not working for anonymous users despite setting cache contexts I have a block plugin that displays different dynamic content depending on whether you're logged in or not. I attempted enabling this by setting the max-age setting in the #cache attribute of my render array to 0 like so

    if ($user-&gt;hasRole('anonymous')) {
            if ($loginrequired) {
                $content = [
                    '#markup' =&gt; '&lt;p&gt; Login required. &lt;a href="/user/login"&gt;Click Here to Login&lt;/a&gt;&lt;/p&gt;',
                ];
                return $content;
            }
    }
    $content = [
        'header'   =&gt; [
            '#markup' =&gt; $header_text,
        ],
        '#cache'   =&gt; [
            'max-age' =&gt; 0,
        ],
        $webform,
    ];
    return $content;


The dynamic content updates for logged in users, but for anonymous users it just displays exactly what the logged in user saw, instead of showing the login required message.

After doing some research it seems that drupal ignores max-cache for anonymous visitors and cache contexts would be required, according to this page

Disable form or block cache for anonymous users?

So I changed my code to reflect this

        '#cache'    =&gt; [
            'max-age'  =&gt; 0,
            'contexts' =&gt; ['user.roles']
        ],


I used user.roles since the content depended on whether the user was logged in or not. This had no effect. I then tried.

        '#cache'    =&gt; [
            'max-age'  =&gt; 0,
            'contexts' =&gt; ['user.roles:anonymous']
        ],


This also had no effect. Why does my method not work? What cache settings are required in order for the cache to be disabled even for anonymous users? Or is there a completely different method I should be using?

Note: I do not want to disable caching site wide. I would like to be able to disable caching on a plugin to plugin basis.

UPDATE: Looking at examples I also tried            

   '#cache' =&gt; [
            'max-age'  =&gt; 0,
            'contexts' =&gt; ['url.path', 'url.query_args']
    ],


but that didn't work either.
The issue was that I had different render arrays set depending on whether the user was logged in or not, but the cache context only applied to one of the render arrays but not the other. If I have different possible render arrays being returned every single one needs to have the cache settings added.

I resolved the issue by changing my code to

if ($user-&gt;hasRole('anonymous')) {
        if ($loginrequired) {
            $content = [
                '#markup' =&gt; '&lt;p&gt; Login required. &lt;a href="/user/login"&gt;Click Here to Login&lt;/a&gt;&lt;/p&gt;',
                '#cache'   =&gt; [
                     'max-age' =&gt; 0,
                     'contexts' =&gt; ['url.path']
                 ],
            ];
            return $content;
        }
}
$content = [
    'header'   =&gt; [
        '#markup' =&gt; $header_text,
    ],
    '#cache'   =&gt; [
        'max-age' =&gt; 0,
        'contexts' =&gt; ['url.path']
    ],
    $webform,
];
return $content;