I have a module with a custom block MyCustomBlock.php that get the nid of the current page and pass the value to the block template:
$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->nid->value;
return array(
'currentnid' => $nid,
);
Inside the template I print the passed value {{ currentnid }}, but it only works cleaning the cache! For example, if I load the page with nid 5 it prints 5, but if I move to page with nid 3 it prints 5. Cleaning the cache and reloading page with nid 3 I get the value 3 printed. I think it is a problem with the template caching system in D8, do you have any idea about this?