How to correctly invalidate a cached node when a menu link content is added/updated/deleted? I'm currently facing an issue:

By following the contrib module's(menu_link_attributes) functionality, I altered the menu item's form to have a visibility by path option(almost the same as in the block config form).

I am saving the input data from the form in the menu_link_content's options column, then unsetting the menu item in hook_preprocess_HOOK() if the conditions are met.

The thing is, we have cached pages which are basically nodes(only on dev environment, the local one works as expected because caching is disabled) and the menu item is unset only after a CC.

I'm trying to add a cache tag to the node so that it's invalidated after I'm done with menu items.

/**
 * Implements hook_preprocess_HOOK().
 */
function prl_menu_item_visibility_preprocess_node(&amp;$variables) {
  // Since all the pages are nodes and all of them are using footer menu, we add the menu_link_content cache tag and invalidate it
  // when a link is saved and supposed to be hidden on specific pages.
  $variables['#cache']['tags'][] = 'menu_link_content';
}


Unfortunately, no success.
The tag you add doesn't exists. You probably want to add the list tag from the menu link content, which is invalidated in all cases when a link item is added, updated or deleted:

$variables['#cache']['tags'][] = 'menu_link_content_list';


But there might be another problem. As I understand your idea is, if you have a cache tag somewhere on the page, the complete page gets invalidated. This is true, but it will not help you. The rebuilt page will contain the cached menu block, if the block has not the correct cache tag on its own. So if you add a cache tag from the menu link to the node you have to do the same vice versa, add a node tag to the menu.

And probably you also need a cache context in the menu block for the visibility.