How to load node object in node preview page? In drupal 8, how do you load the node object in hook_preprocess_page() when on the node preview page?

I had a bug come up where this standard way of loading the current node object was failing on the preview page:

$node = \Drupal::routeMatch()-&gt;getParameter('node');
If you need to use the node object in hook_preprocess_page() on the preview page, you need to use the "node_preview" parameter, instead of the "node" parameter:

function mymodule_preprocess_page(&amp;$vars) {

  $route_name = \Drupal::routeMatch()-&gt;getRouteName();

  if ($route_name == 'entity.node.canonical') {
    $node = \Drupal::routeMatch()-&gt;getParameter('node');
  }
  elseif ($route_name == 'entity.node.preview') {
    $node = \Drupal::routeMatch()-&gt;getParameter('node_preview');
  }