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


1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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