Hello,

I just had an issue that I thought was a bug but turned out to be a lack of knowing Drupal 8. While the documentation seems to be confusing/lagging and I feel this is going to be one of the biggest issues with documentation in Drupal 8, and needs to be expanded.

My issue - https://www.drupal.org/node/2587489

After reading https://www.drupal.org/developing/api/8/cache/contexts I am still really confused as to what I need to do. After playing around I figured out what to do. But it took me over 8 hours and many core var_dumps, and I ended up just leaving the NullCacheBackend in for now.

I'm not sure how exactly is best to explain this, but I feel it needs expanding.

Maybe we should add some details to any of the theme_pre,post,etc_hooks to expand on editing/modifying cached variables, or maybe we need to expand on the caching documentation.

Anyone have any ideas? Sorry for this long winded post.

Comments

dubcanada created an issue. See original summary.

jhodgdon’s picture

Version: 8.0.0-rc1 » 8.0.x-dev

Thanks for making an issue! You could have also repurposed your original issue, but that is fine.

So, let's see what might need documenting, and see if/where.

a) On that other issue, you found that if you added something to a render array without any cache information, it was automatically cached. We do have some documentation caching in render arrays in the Render API topic:
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api....
and I think that what is there is accurate, but limited. That is probably OK.

In your case you might have thought it didn't apply because you didn't put #cache or anything like that in your render array, but since it was part of node preprocessing, the node as a whole had cache tags so the whole thing was going to be cached.

So assuming you knew it applied, I think reading the short descriptions you'd figure out you needed a cache context probably? Hopefully...

b) Either that, or you'd figure you needed more information. So that documentation page points to
https://www.drupal.org/developing/api/8/render/arrays/cacheability
for more information. That page explains in more detail about cache stuff, and then points you to
https://www.drupal.org/developing/api/8/cache/contexts
for details on cache contexts (which again I think you'd hopefully realize you'd need to learn more about).

Hm... That page could probably use some love. As documentation goes, I didn't find it all that easy to use for practical purposes.

c) The other starting point on api.drupal.org, if you knew you had cache issues, would likely be
https://api.drupal.org/api/drupal/core!core.api.php/group/cache/8
That page also has a short section on cache contexts, also pointing to https://www.drupal.org/developing/api/8/cache/contexts for more details.

So. My feeling is that we need to make
https://www.drupal.org/developing/api/8/cache/contexts
more useful to someone in your situation. It looks like from your report that you came to the same conclusion, that this page needs to be more useful/practical to the developer.

Anything else you think we need to improve? Any thoughts?

handkerchief’s picture

I also have problems with the documentation.

How can i set a variable in the function theme_preprocess_page() that changes when a cookie changes?

for example:

$variables['testvariable'] = $_COOKIE['testcookie'];
$variables['#cache']['contexts'][] = 'cookies:testcookie';

in page.html.twig
{{ testvariable }}

or

$variables['testvariable'] = [
    'value' => $_COOKIE['testcookie'],
    '#cache' => [
      'contexts' => 'cookies:testcookie',
    ],
  ];

in page.html.twig
{{ testvariable.value }}

I know I need cache tags, but which ones? I also set the max-age to 0, but nothing works. The value doesn't change for anonymous users.

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

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

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

Jeff Burnz’s picture

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.

traviscarden’s picture

aerrasti’s picture

I'm stuck in the same problem. Did you figure out how to solve the cookie/contexts problem ?

I also change the templates variables depending on a specific cookie value like :


function hook_preprocess(&$variables, $hook) {
    $gamma_cookie = \Drupal::request()->cookies->get('gamma_rol');
    $variables['gamma_rol'] = $gamma_cookie;
    $variables['#cache']['contexts'][] = 'cookies:gamma_rol';
}

But is not working for anonymous user when the cache is activated.
Any help ?

thank you

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.

owenmck’s picture

One more person here, I think with the same problem. Is this PEBKAC?

Aim:

I want to invalidate the anonymous user's (log-in) page cache after, say, 120sec,

Method

  1. In function theme_preprocess_page() I added
\Drupal::logger('custom_userlogin')->debug(__METHOD__);
$uid  = \Drupal::currentUser()->id();
if ( !$uid || $uid <= 0) {
  $variables['#cache']['max-age'] = 120;
  $variables['last_cached'] = date("Y-m-d H:i:s");
}

Then, in page.html.twig I insert the following little test.
Last cached: {{ last_cached }}

  1. As 'admin' in Firefox I clear the Cache,
  2. As anonymous user in Chrome I load the site, using XDEBUG to trace it.
  3. Capture starting values using XDEBUG

Starting values

$variables['#cache'] ['contexts'] = ["user.permissions"];
$variables['#cache']['max-age'] = 120;
$variables['last_cached'] = "2017-08-30 14:16:42";
  1. Wait 2 min and refresh the page (CTRL + F5) from the server.
  2. Wait another 2 min and do it again

Expected

The rendered 'last_cached' value changes to a more recent string each time I hard refresh the page after more than 2 minutes.
The admin user's log detects each new access into the logic of theme_preprocess_page().

Found

It does not change. Nor does the admin user's recent log messages detect any new visit to theme_preprocess_page().

Conclusion

  1. Subsequent hard refreshes more than 2 min later are served using the same long-term cache copy,
  2. the logic does not again traverse theme_preprocess_page() until the cache is cleared.

Stumped. Where are the cache instructions being overruled?

owenmck’s picture

More

[edited]
I thought I had an answer but alas I did not.
Still stuck.
I would love it if i could make the Internal Page Cache respect the max_age value set via the admin Performance page!

This, it seems is a known issue.

See

  1. Bubbling of elements' max-age to the page's headers and the page cache
  2. Time based cache does not work as expected for anonymous users

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.

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.

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.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank you for creating this issue to improve Drupal.

We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

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.