1

So I am having trouble understanding the caching of variables set in hook_preproccesses for anonymous users. It seems that the entire page is cached, and not rebuilt, even when set to no caching. With the following simplified example I am troubleshooting:

/**
* Implements hook_preprocess_node().
* @param $variables
*/
function hook_preprocess_node(&$variables) {

  $variables['testing_1'] = [
      '#markup' => date('m/d/Y h:i:s a', time()),
      '#cache' => ['max-age' => 0]
  ];

  $variables['testing_2'] = date('m/d/Y h:i:s a', time());
}

then in twig template:

{{ testing_1 }}

{{ testing_2 }}

Both seem to work fine for logged in users, but these variables never seem to update for anonymous users except upon:

  • Manually clearing cache
  • Saving content on the node, which then clears node cache.

Even when page caching is set to "no caching" on /admin/config/development/performance, but that does not seem to matter. Its as though the hook_preproccess does not run for anon users once first page hit from anon user happens.

It seems like from Anonymous user cache control and others, this is due to internal page cache module. Uninstalling this DOES solve the problem, but I am unsure that this is the best way to solve this issue, especially since it is enabled by default. Per the docs, internal page cache says "This module assumes pages are identical for all anonymous users". This is true, I simply want the pages to rebuild (rerun the preproccess hook) for ALL anon users when page cache (set on /admin/config/development/performance) clears.

After a day or so of troubleshooting, I am considering simply creating a cron job to flush caches at this point, but there has to be a better way I am missing. What is the best way to ensure that pages rebuild for anonymous users after a set time?

0

You may have encountered a core bug where a page cache is not respecting the max-age, or in other words, max-age is not bubbling up to the page cache. There's a workaround on the issue summary but the workaround has a few problems of its own so make sure you consider the implications carefully. The bug issue is postponed until 3 of the linked issues are resolved. Hope this helps.

  • Thanks @beebee thats extremely helpful, not sure how I missed that in my searches. I'm glad to see some recent movement on it, though its interesting to see its been a known issue for years. This definitely gives me some directions to pursue in the meantime. Thanks again! – Daniel Rogers Aug 22 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.