Score:0

Excluding individual page components from caching

cn flag

I can't seem to find a straight answer online about whether it is possible to exclude just one component of a page from being cached.

My component appears on all pages of the site and comes from a custom theme function rendered in page.html.twig from the following in mytheme_preprocess_page:

$alerts_renderable = [
'#theme' => 'my_alerts',
];
$variables['alerts'] = $alerts_renderable;

I've tried adding #cache => ['max-age' => 0] in that render array to no effect. I read elsewhere that changing max-age has no effect on anonymous visitors. Has anyone had success in ever doing this?

The component is an alert bar that site admins can enter text via a settings page on the backend. That message appears in the alert bar on every page of the site. Currently, when admins change the message, it requires a cache clear for the new message to be publicly visible

4uk4 avatar
cn flag
You don't need cache max-age, you need the cache tag of the config object saved by the settings page.
4uk4 avatar
cn flag
See https://drupal.stackexchange.com/questions/294839/how-to-clear-the-a-block-cache-programmically
Score:0
id flag

The current limitations on the max-age implementation are documented, as are the workarounds.

Until these (and perhaps other) issues are resolved, beware that setting max-age on a render array included in a page is insufficient to ensure that anonymous users will see a new version after the max-age as elapsed. In the meanwhile, the Cache Control Override contributed module tries to mitigate the problems. You might also have more luck setting a custom cache tag on pages with time-dependent content and invalidating those cache tags manually via hook_cron(). Good luck!

The paragraph above contains hyperlinks to the referenced resources when viewed on the documentation page.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.