Template preview not working properly
While previewing a node, the node parameter is not "upcasted" from an integer to a node object. But you can easily do this yourself in the preprocess function, just add those lines before:

  $router = \Drupal::routeMatch();
  $node = $router-&gt;getParameter('node');
  if (is_numeric($node)) {
    $node = \Drupal\node\Entity\Node::load($node);
  }
  //now you always have "real" node object (instanceOf Drupal\node\NodeInterface -&gt; true), 
  //no matter if preview or normal viewing