ViewPageController doesn't provide complete cacheability metadata

Created on 23 December 2016, almost 8 years ago
Updated 21 September 2024, 2 months ago

Problem

Currently when a view with a page display customizes its cacheability metadata this is not output by the page controller and the page is not correctly cached.

Only cacheability metadata which is related to query access checks and displaying of contextual links is output. Any other metadata is ignored.

An example: I have a view of which the results are rendered differently if a user is an administrator in Organic Groups. So my view varies by the role my user has in Organic Groups. I alter the view with hook_views_pre_view() and add the 'og_role' cache context to its displays.

When I load the page with cleared caches a correct render cache entry is made in the database, marked with the correct cache context keys. However this cache entry is now never used, since ViewPageController outputs a render array which has a hardcoded set of cacheability metadata which lacks my 'og_role' cache context.

ViewPageController::handle() retrieves a basic array of metadata from Page::buildBasicRenderable() and enriches it by calling views_add_contextual_links(). The actual cache contexts that are present on the View entity are not included.

Proposed solution

We can solve it by storing the cacheability metadata on the router object using custom property like _view_display_cache_contexts. The router object is populated in PathPluginBase::getRoute(). The full entity is loaded at that point so we can retrieve the cacheability metadata and provide it to the router. We can then use these custom properties to output the full cacheability metadata in ViewPageController::handle().

πŸ› Bug report
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 10 hours ago

Created by

πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands Lendude Amsterdam

    Nothing much has changed in the calling code here, so this might still be valid, but the currently provided steps are a bit too vague to easily start work on this. Some more concrete steps would really help.

    Are there steps to reproduce this with just core? Probably not since you need to add additional cache_contexts I assume?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    In my experience a preprocess hook works fine, as you can vary $variables['#cache'] there, same for #attachments

  • Status changed to Closed: works as designed 2 months ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    This issue hasn't had much discussion in 8 years and a views maintainer asked for more information over a year ago and none has been supplied. And larowlan has supplied a way to resolve this. Therefore, I am closing this.

    If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue β†’ (starting from "Install Drupal core").

    Thanks!

Production build 0.71.5 2024