I'm not 100% sure this is a cache issue, or a views issue, feel free to move it if needed :) I suspect it's views that does the building and gathering of cached content.

Running Drupal 8.2.4 with the theme hook suggestions patch from issue 2118743, Internal Dynamic Page Cache and Internal Page Cache are both enabled. If I disable both, the bug does not occur.

The case :

I have two views, who both render the same entity in the same display mode BUT since it's two different views we wanted to use two different templates, hence the extended template suggestions.

One view creates a block with two news items to be used on the frontpage, A node set a in the settings, and the news is rendered as entity in teaser mode, using a template called node--view--frontpage--news.html.twig

One view created a page with news items, also in entity render, with the teaser display of the news node, but uses a template called node--news--teaser.html.twig

If I clear all caches, and open the front page, both news both blocks show op perfectly, however when you go to the news overview page, both news items are rendered in the output style of the frontpage block. Same works vice versa, clear cache visit overview, the frontpage shows the two items in overview style.

Did some digging in the cache_render table, and noticed that the rendered entity is stored twice. Once with a #markup key, and once with the #cache_redirect and a #cache context. I unserialized the keys of the #cache contexted and noticed that the name of the view is not used in that context.

The CID of both entries are almost the same.

#markup has
entity_view:node:290:teaser:[languages:language_interface]=en:[languages:language_url]=en:[theme]=customer:[timezone]=Europe/Amsterdam:[user.permissions]=b1d6062c14be41e5c86bbcd3e73a0b3f852f71d1eddb603347844a225e28d9a9

#redirect has 
entity_view:node:280:teaser:[languages:language_interface]=en:[theme]=customer:[user.permissions]=b1d6062c14be41e5c86bbcd3e73a0b3f852f71d1eddb603347844a225e28d9a9

I think that views should add the view machine name, or the template name to the cache tags of the rendered entity, but i'm not confident enough if this is actually handled by views or by the caching system.

A workaround could be, introduce a new display mode for that entity, but that feels dirty because the only difference between those two output's is one class on the wrapper DIV in the block code.

Comments

Rene Bakx created an issue. See original summary.

Rene Bakx’s picture

Looks like contrib is trying to fix this issue to : https://www.drupal.org/project/views_custom_cache_tag

Berdir’s picture