How do I stop the node from caching Sometimes when I run below code in the hook_preprocess_menu() of a node, I am getting a previously loaded node instead of my current node.

\Drupal::routeMatch()-&gt;getParameter('node');


What should I do to stop this?
If you add node data in a preprocess hook, then you need to add the cache tag of the node:

$variables['node_title'] = [
  '#markup' =&gt; $node-&gt;getTitle(),
  '#cache' =&gt; [
    'tags' =&gt; $node-&gt;getCacheTags(),
  ],
];


If this node is from a route match, then you need to add a cache context for the route as well:

$node = \Drupal::routeMatch()-&gt;getParameter('node');
$variables['node_title'] = [
  '#markup' =&gt; $node-&gt;getTitle(),
  '#cache' =&gt; [
    'tags' =&gt; $node-&gt;getCacheTags(),
    'contexts' =&gt; ['route'],
  ],
];