Problem/Motivation

Views using the taxonomy term filter (`taxonomy_index_tid`) currently add the 'user' context to the view, eliminating cacheability via dynamic page cache in most circumstances. This appears to have been a catch-all solution for missing tests around term access.

Proposed resolution

Remove the user context and update existing views configuration via a post-update hook.

Remaining tasks

None

User interface changes

None

API changes

None

CommentFileSizeAuthor
#9 figure_out_whether_term-2352175-9.patch627 bytesberdir

Issue fork drupal-2352175

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jibran’s picture

wim leers’s picture

catch’s picture

Title: Figure out whether term access (or other query based access checking) is implemented. » Figure out whether term access (or other query based access checking) is implemented before adding per-user cache context
Version: 8.0.x-dev » 8.1.x-dev
xjm’s picture

#2677234: Views Page display with filter "Has Taxonomy Term" adds unnecessary 'user' cache context was closed as a duplicate of that issue as @jibran pointed out. Note that while that issue was marked major, @catch, @alexpott, @effulgentsia, @Cottser and I actually agreed that this was better as a normal-priority issue. There is no risk of cache poisoning, just a possibility that the cache invalidation might happen more than it needs to, and the scenario is uncommon as well as not really supported by core out of the box. So we agreed "normal task" was correct for this issue.

Thanks @jibran and @Wim Leers!

Let's also incorporate the additional information from that issue as well as @jibran's tests here.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Two things are not correct about this sentence:

> that the cache invalidation might happen more than it needs to, and the scenario is uncommon as well as not really supported by core out of the box

* It's not about invalidation, it's about contexts/variations. This means that views with a filter like this creates caches for every single user. This is also interesting because exposed forms inherit the views cache contexts, so they too end up being per-user and then they are placeholdered.

* This cache context is *always* added, not just for the rare scenarios where people actually have term access. Not doing it unless it is really needed is the point of this issue.

This currently even affects search-api based views with term filters, where it is even more unlikely that it needs to be cached by-user.

berdir’s picture

Status: Active » Needs review
StatusFileSize
new627 bytes

Simple patch to disable that for sites that know they don't need it. Also wondering if we have any tests that actually rely on it, which I doubt.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

gregsullivan’s picture

I encountered this with a view that simply grouped Country nodes by a Region taxonomy, rendering the view uncacheable.

I strongly agree with @Berdir: The rare scenario requires the cache context; the common scenarios that would benefit from caching won't be cached until the cache context is only added when it's needed, or until there's a way to opt out.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

#8's corrections to #4 are correct.

This challenge with cacheability metadata not being optimal for all use cases is not unique to this particular @ViewsFilter plugin.

bkosborne’s picture

Priority: Normal » Major

Yikes - I traced down this pesky cache context when trying to figure out why a seemingly innocent views block on a page was making the entire page uncacheable in dynamic page cache*! This is a very commonly used views filter - I hope no one minds bumping this to major. It was a major time sink tracking this down as I never would have thought it was a taxonomy term filter that was adding the context.

*I'd love if someone with more expertise in dynamic page cache can help explain why this happens. I know the "user" context is by default in the auto_placeholder_conditions list, which I thought just meant that whatever render data declared this cache context would be placeholdered and then rendered later, but still allow dynamic page cache to create a cache entry. But that's not what happens. Dynamic page cache will just not cache anything at all if the response has the "user" cache tag.

berdir’s picture

Yeah, you're not the first to find this :)

The auto placeholder stuff can only happen if the problematic element is in an area that can be placeholdered, specifically a regular block and not the main content region.

I still think we should just remove this, optionally combined with checking for a term_access specific hook implementation maybe? So that at least only sites that use that are affected. Which is probably very, very few.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jonathanshaw’s picture

I believe that the user cache context is now always required, because #3101738: Exposed term filters should not show term options that the user does not have access to added a check for the 'administer taxonomy terms' permission.

Possibly this means that this issue should be closed, as there is no straightforward route to changing the cache contexts without reworking altogether how access is handled in this taxonomy exposed filter.

berdir’s picture

Checking for a permission does _not_ require the user cache context, just user.permissions, which is already there by default.

alexpott’s picture

A really odd thing about this that had me stumped for a little is that \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTidDepth adds the user context but \Drupal\taxonomy\Plugin\views\argument\IndexTidDepth which is very very similar code - does not. I'm glad it doesn't because the site I'm performance profiling would have worse performance but it is odd.

berdir’s picture

I think the mean reason the filter has it is exposed filter forms, which lists all terms of a vocabulary. We could as a minimal first step move the context just to the form structure and not the plugin.

kim.pepper’s picture

Issue tags: +#pnx-sprint
fgm’s picture

Version: 8.9.x-dev » 9.3.x-dev

Just found out a site which was burnt by it, having a Views block using that filter in the footer of the view building their main page, killing the cacheability.

Bumping the version top current core. Of course as @Wim Leers says, this fixes the issue, but this can not be introduced in core in its current #9 state: the rare case where it is needs still needs to be catered for.

fgm’s picture

Status: Needs review » Needs work

Accordingly, resetting to CNW.

bryanmanalo’s picture

After I applied the patch, I thought it would immediately remove the user cache context on the view. But you have to resave the view after applying the patch (and export to config). I thought this was dynamically computed. The cache context is re-computed on view save.

Hope this helps.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

b_sharpe’s picture

Well this was an interesting one to XDebug :D

I agree this should just be removed. The user.permissions context should cover variations here and it doesn't appear to provide any additional benefit that wouldn't be provided by #1947270: Add test coverage for the term_access query tag

This is quite a large issue when a view is a page display or is embedded as part of the main content region and thus completely uncacheable for dynamic page cache.

b_sharpe’s picture

Status: Needs work » Needs review

Welp, it didn't break any tests, so I'd be in favor of getting this in and dealing with test coverage in the other ticket. Marking for review.

catch’s picture

Status: Needs review » Needs work

Views' default cache plugin uses the generated query as the cache key - see https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%...

hook_query_alter() implementations are also able to add cache contexts, see for example: https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/fu...

So we should definitely remove this, however we need a post update + presave here to resave all views so that the cache contexts get recalculated. Other view updates + ViewsConfigUpdater should have examples.

Agreed it doesn't need test coverage, we'd just be testing that the code doesn't do what it used to at a particular point in time, there wouldn't really be positive assertions to make.

b_sharpe’s picture

Status: Needs work » Needs review

Yup, makes sense. Normally just a view re-save would've done here, but it appears the re-calculating of cache meta is only done via UI. MR has been updated.

smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for IS so moving to NW for that.

But with a post_update hook will this require test coverage?

Also new functions may need a change record, could be wrong there.

b_sharpe’s picture

Title: Figure out whether term access (or other query based access checking) is implemented before adding per-user cache context » Taxonomy views filters should not have user context
Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -#pnx-sprint

Updated issue summary and removed tag. I agree with @catch that there is no test coverage needed here as we're removing code, not adding. Tests for what this originally existed for should go into #1947270. I don't really see any reason for a change record here either, back to Needs Review.

smustgrave’s picture

Status: Needs review » Needs work

I don't see the changes to the issue summary? Maybe they didn't save?

b_sharpe’s picture

Issue summary: View changes
Status: Needs work » Needs review

Not sure what happened there, it change the title but not summary.. trying again

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks much better!

bkosborne’s picture

Looks good to me too! this just bit me again so I'd be happy to get this committed

  • catch committed 80e037e3 on 11.x
    Issue #2352175 by b_sharpe, Berdir, smustgrave, Wim Leers, catch,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x.

MR has conflicts with 10.1.x, and there's a post update in here, so leaving in that branch - it'll be released with 10.2.0

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

pawel.traczynski’s picture

For anyone looking for a fix, exporting the view to config/sync as yml, removing the cache_metadata.contexts.user and importing the config back to site fixed the caching issue for me.