Score:1

Custom block is still caching for anonymous users despite getCacheMaxAge() being set to 0 and can't figure out why

ng flag

I have a custom Drupal 8 block with class with build() and getCacheMaxAge() functions. getCacheMaxAge() is set to 0 but it is still being cached for anonymous users.

If I put debug code in the build() function I can see it being called when I'm logged in but not for anonymous users.

My code is below. We do have Memcached installed. Would that be causing the issue?

The only other caching modules are core.

<?php
class HeaderPhone extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {

    $info = geo_location_blocks_get_region();
    $region = $info['region'];
    $tid = $info['tid'];

    return array(
        '#theme' => 'geo_location_header_phone',
        '#region' => $region,
        '#view' => views_embed_view('locations', 'block_3', $tid),
      
    );
  }

  /**
   * @return int
  */
  public function getCacheMaxAge() {
    return 0;
  }
}?>

  
Jaypan avatar
de flag
You might need to add a max-age to the render array as well, using `'#cache' => ['max-age' => 0]]`.
quantumized avatar
ng flag
Thank you Jaypan, I tried adding #cache to the render array as suggested but the block is still caching for anonymous users.
Jaypan avatar
de flag
Re-reading your post, memcached could be an issue. You could try disabling it and seeing if the problem persists. Or testing on an environmnent with memcache disabled. The other possibility is the page cache may be caching the whole page for anonymous users.
quantumized avatar
ng flag
I ruled out memcached. I tested the issue on a site copy on a different server without Memcached and the issue is still there.
Score:5
cn flag

Setting cache max-age to 0 doesn't work if you still have the core module Internal Page Cache enabled.

Read the documentation why you probably want to disable this module or deliver the block with a different method:

Websites that serve personalized content to anonymous users (dynamic, per-session, e.g. a shopping cart) will want to disable the Internal Page Cache module. This module assumes pages are identical for all anonymous users. Those websites can still take advantage of the Dynamic Page Cache module though, or can alternatively do their personalization using JavaScript + AJAX.

https://www.drupal.org/docs/administering-a-drupal-site/internal-page-cache

quantumized avatar
ng flag
Thank you. the Internal Page Cache module was the issue.
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.