How do I invalidate cache of an embedded view? I have a node type "Article" that contains a repeating Paragraphs field. One of the paragraph types let's the user embed a view using a entity reference field. When rendering an Article node containing a views reference I embed the selected view in hook_preprocess_paragraph like this:

$view_id = $paragraph-&gt;field_view-&gt;target_id;
$view = \Drupal\views\Views::getView($view_id);
$view-&gt;setDisplay('embed_1');
$vars['content'] = $view-&gt;preview();


This is working fine except when I enable caching. For example one of my views lists nodes of the type "Event". When I add a new Event it does not show up in the listing until I clean the cache. I could force clean the cache when inserting / updating / deleting an "Event" but I would much rather use Drupal 8's cache tag system and only invalidate the cached content that contains event lists. The problem is I cannot get it to work.

So far I have tried adding cache tags to the node, the view, and the paragraph  in hook_preprocess_paragraph and hook_preprocess_node like this:

$vars['paragraph']-&gt;addCacheTags(['node_list']);
...
$view-&gt;storage-&gt;addCacheTags(['node_list']);
...
$vars['node']-&gt;addCacheTags(['node_list']);


According to this page node_list can be used for node listings to invalidate the cache whenever a node is updated. But it has no effect. Maybe the preprocess hooks are to late to add cache tags?

What am I missing?

Edit:

After some more testing I found that if I edit an event, the changes can be seen in the listing and if I remove an event it disappears from the listing. The problem only occurs when adding new events.
Try to use buildRenderable(), that should ensure that the necessary cacheability metadata is added automatically.

$vars['content'] = $view-&gt;buildRenderable();


Hint: You might be interested in my Views Custom Cache Tag project. The node_list cache tag is invalidated every time any type of node is saved, even though your view only changes when an event changes. Using that module and a bit of custom code as shown on the project page, you have fewer cache invalidations and better performance.