Mar 30, 2017 in Snippets, Drupal 8

When creating custom blocks, sometimes you need to make sure the block is never cached f.e. when including dynamic links. 

Add the snippet below to your custom block to make sure it is never cached:

class MYCUSTOMBLOCK extends BlockBase {
  /**
   * {@inheritdoc}
   */
    public function build() {
        return array(
            '#markup' => ""
        );
    }
    /**
     * {@inheritdoc}
     */
    public function getCacheMaxAge() {
        return 0;
    }
}

More about block caching?