1

I have a little probleme with my cache block.

This is my build function :

return [
  '#markup' => time(),
  '#cache' => [
    'max-age' => 10
  ]
];

I found this specification for block cache.

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return 20;
  }

Because in parent class we have $max_age = Cache::PERMANENT;

So, when I'm connected my rendered cache refresh each 10 seconds. But when I'm a anonymous user my rendered block never change.

I don't find the solution.

Big pipe isn't install, I have aggregations css and js but I don't have max age in admin/config/development/performance.

Finnaly I want a block without cache because this block will rendered message with close button. When the user click on the close button, I set a cookie for return an empty bluid in my block.

Do you have an idea ?

Thank you

2
  • In your custom block template, are you rendering the {{ content }} variable in full? If not, cache tags may not be bubbling up.
    – Kevin
    Commented Feb 13, 2017 at 16:57
  • I havn't custom block template for moment. I rendered also #markup with #cache key
    – KaelFaz
    Commented Feb 13, 2017 at 17:06

2 Answers 2

3

You don't have to worry about the values which are set for cache max-age in other places, because when these are merged the lower time that you have set wins.

But you can't control with max-age the page cache for anonymous users. You have to use \Drupal::service('page_cache_kill_switch')->trigger(); in the block build method to disable the page cache or you can simply uninstall the module Internal Page Cache if you plan to use the block on all pages.

but I don't have max age in admin/config/development/performance

You should set this to the same max-age to control the browser cache and proxies.

2
  • I'm convinced you're a wizard.
    – Kevin
    Commented Feb 13, 2017 at 22:21
  • See drupal.org/node/2352009 for the core issue that is trying to change this.
    – Berdir
    Commented Feb 13, 2017 at 23:07
0

Cache Control Override plugin intends to solve the problem with an anonymous user cache control. After installation, both methods will work:

return array(
  '#markup' => date("Y-m-d H:i:s"),
  '#cache' => array('max-age' => 10).
);

or

public function getCacheMaxAge() {
    return 10;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.