Disable cache a custom block for anonymous user I have a custom block, it displays in every pages with dynamic data calculates in every refresh, How can I disable cache for this block for anonymous users?

I try with:

public function build() {
  return [
    '#markup' =&gt; time(),
    '#cache' =&gt; [
      'max-age' =&gt; 0,
    ],
  ];
}


It won't work.

Edit:
I don't want to disable the cache for the entire page, I just need a specific block not to be cache.
If you want to completely disable caching for the block, use this function in your block class:

  public function getCacheMaxAge() {
    return 0;
  }


I've never found in the documentation why

'#cache' =&gt; [
      'max-age' =&gt; 0,
    ],


doesn't work with blocks but, like you discovered, it doesn't seem to.