Block caching issue on twig I've created a new custom module and I used Facebook SDK to retrieve the latest post from a page.
Here is my .module file:

function social_theme($existing, $type, $theme, $path){
  return array(
    'block__feed_feed' =&gt; array(
    'render element' =&gt; 'elements',
    'template' =&gt; 'block--instagram-feed',
    'base hook' =&gt; 'block'
  )
}
function social_preprocess_block(&amp;$variables){
 if ($variables['elements']['#configuration']['id'] === 'facebook-feed'){
   $fb = new \Facebook\Facebook([
     'app_id' =&gt; {$app_id},
     'app_secret' =&gt; {$app_secret},
     'default_graph_version' =&gt; 'v2.10',
     'default_access_token' =&gt; {$app_id}.'|'.{$app_secret},
   ]);

   $request = $fb-&gt;request('GET', '/'.{$page_id}.'/posts?fields=created_time,reactions,message,comments{comment_count,like_count},likes&amp;limit=4');
   try {
     $response = $fb-&gt;getClient()-&gt;sendRequest($request);
   } catch(Facebook\Exceptions\FacebookResponseException $e) {
     echo 'Graph returned an error: ' . $e-&gt;getMessage();
     exit;
   } catch(Facebook\Exceptions\FacebookSDKException $e) {
     echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
     exit;
   }
   $posts = $response-&gt;getDecodedBody();
   $facebookposts = [];
   for($c=0 ;$c&lt;$posts_no ;$c++){
     if(isset($posts['data'][$c]['reactions'])){
       $likes_count = count($posts['data'][$c]['reactions']['data']);
     }
     else{
       $likes_count = 0;
     }
     if(isset($posts['data'][$c]['comments'])){
       $comments_count = count($posts['data'][$c]['comments']['data']);
     }
     else{
       $comments_count = 0;
     }
     array_push($facebookposts,['post_text'=&gt;$posts['data'][$c]['message'],'likes_count'=&gt;$likes_count,'comments_count'=&gt;$comments_count]);
   }
   $variables['elements']['#cache']['max-age'] = 0;
   $variables['fbposts'] = $facebookposts;
   $variables['likes_comments'] = $likes_comments;
 }
}


and my block plugin code:

public function build() {
  \Drupal::service('page_cache_kill_switch')-&gt;trigger();
  return array(
    '#markup' =&gt; ' ',
    '#cache' =&gt; ['max-age' =&gt; 0,],
  );
}


and this is my block template file:

&lt;ul&gt;
{% for post in fbposts %}
 &lt;li&gt;
  &lt;div class="facebook"&gt;{{post.post_text}}&lt;/div&gt;
    &lt;div class="likes-comments"&gt;
     &lt;span id="comments"&gt;{{post.comments_count}}&lt;/span&gt;
     &lt;span id="likes"&gt;{{post.likes_count}}&lt;/span&gt;
    &lt;/div&gt;
  &lt;/li&gt;
 {% endfor %}
&lt;/ul&gt;


Now my problem is that the code is working perfectly, but when a new post is added it will not change on the website unless I clear the cache!!!
I tried to give the block '#cache' =&gt; ['max-age' =&gt; 0,] but it didn't work, I tried \Drupal::service('page_cache_kill_switch')-&gt;trigger(); on the hook_preprocess_block but id didn't work too.

What shall I do?
You have to render the content build() returns:

block.html.twig:

...

{{ content }}


Even if it's only an empty markup string. Otherwise the cache max-age can't bubble up.

A better approach would be to return from build() a custom template, which you use for the html markup instead of the block template. See https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module

Then you don't need a preprocess hook and caching works with the default block template.