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?
['max-age' => 0
not closed off is just a typo right?