Block Caching

Articles & Reference

Examples

Disable caching

  • Useful to add when debugging
$build = [
  '#cache' => [
    'max-age' => 0,
  ];

By route

  • A version of the block is cached for each path, including paths with parameters
    e.g.: node/{id}, my-entity/{id}
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts(), array('route'));
  }