Problem

When block cacheability was introduced in #2375695: Condition plugins should provide cache contexts AND cacheability metadata needs to be exposed this was implemented by merging in the cacheability metadata of all potential displayable blocks. The reason for this is clear, a block might currently not be displayed, but it might be when e.g. the cache context changes, or when a relevant cache tag is invalidated. So it makes sense to put the merged cacheability metadata of every block that is potentially displayed onto the blocks' region.

However this has an unwanted side effect: if a region contains even a single block that has max-age = 0 then this will mean that this region will never be cacheable, effectively disabling caching on every page that displays this region. For many sites this will mean that a single uncacheable block will turn the entire website uncacheable.

We should mitigate this by figuring out which blocks are actually potentially visible. For example, if a website displays (uncacheable) facet blocks on the search page, then this should only cause the max-age = 0 to be applied to the search page, not to all pages.

Similarly, if a block has its visibility set to only display on nodes of type "Article", then this should not affect any pages that don't show nodes, or show nodes of other types.

The problem is located at the end of BlockPageVariant::build() where the cacheability metadata of all blocks is merged in and applied to the root of the render array.

Proposed resolution

Up to debate.

Comments

pfrenssen created an issue. See original summary.

berdir’s picture

Was confused at first because we only consider access cacheablity metadata there.

But of course the last line \Drupal\block\BlockAccessControlHandler::checkAccess() is the problem, that adds the block cacheablity metadata.

I think we can try to change that to specifically only add the cache tag, so we update it for things like saving the block and changing its configuration, but not weird cache contexts and cache max age.. we shouldn't care about that on that level if it was not *explicitly* added to the access result.

fabianx’s picture

I think the main problem with that is that the auto-placeholdering can't kick-in, because the access check is happening before actually rendering it, which is both good and bad.

Good for sections of the page being dependent ob blocks visible or not, bad for auto-placeholdering, which was created to solve this problem.

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

mxr576’s picture

So is there a new/old idea how to fix this issue?

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.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.

larowlan’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update, +Needs steps to reproduce, +Bug Smash Initiative

Adding some tags

catch’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Active
Issue tags: +Needs tests
Related issues: +#2352009: Bubbling of elements' max-age to the page's headers and the page cache

The code hasn't changed here so pretty sure this is still valid.

This is a microcosm of the problems with #2352009: Bubbling of elements' max-age to the page's headers and the page cache, so adding as a related issue.

One possible approach here would be to merge cache tags onto the region (since those will affect whether a block is displayed or not), but not max age or cache contexts (which generally won't, or should be explicitly in the access checks if they do) - this is what Berdir suggests in #2 as well if I'm reading this correctly.

IMO easiest step here would be a failing test that puts a block with max-age 0 into a region, and then checks the cacheability of the region, then we can evaluate the fix against that.

geek-merlin’s picture

>
Throwing away part of cacheability (context and max-age) by not bubbling up is a break of contract and a BC break. Domain context is a simple example.

Of course a completely uncacheable page is bad. Fabianx pointed in #3 to autoplaceholdering.

geek-merlin’s picture

Issue summary: View changes
berdir’s picture

We would still respect any cacheablity added to the access result (and make it uncacheable if necessary). We only support autoplaceholdering for single blocks once we know whether or not a given block is allowed. All blocks of all regions are rendered at once, making regions auto-placeholderable would be a huge change

geek-merlin’s picture

Thanks for elaborating! Maybe the proposed solution needs a more thorough explanation, and a draft CR with its ramification.

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.