Editorial illustration: a Drupal custom block branches by user permission, refreshes when content tags change, and expires on its chosen max-age.

A custom Drupal block can be fast in one test and wrong for the next visitor. The common cause is not that caching is broken; the render array has not told Drupal what makes its output vary or when it becomes stale.

Drupal cache metadata is easiest to reason about by asking three separate questions: who or what changes the output?, what data invalidates it?, and how long is it valid? Those map to cache contexts, tags, and max-age. This is a practical mental model for a custom block, with an example and a debugging path.

Summary

  • Add a context when output varies by request state, such as permissions or interface language.
  • Add tags for the content or configuration whose changes should invalidate the cached output.
  • Set max-age only when time itself makes the output stale; 0 disables caching for that render item.
  • Prefer cacheable entities and dependencies that carry metadata automatically, then inspect the bubbled response metadata.

Begin with the output, not a cache setting

Imagine a block that displays a notice based on the current user’s permission and a configuration value. Before writing #cache, list the inputs:

Output depends on Cache metadata question
Permission or another request property Which context varies the representation?
A node, user, file or configuration object Which tags invalidate it when that dependency changes?
A deadline or short-lived external value What max-age bounds its freshness?

Contexts vary a cached result, much like an HTTP Vary rule. Tags identify dependencies that can invalidate cached output. Max-age expresses a time limit in seconds; Drupal’s permanent value is -1, and 0 means not cacheable.

Example: a notice visible to editors

If a block says “Review pending content” only to users with a particular permission, the output varies by access context. A conceptual render array could include:

$build = [
  '#markup' => $show_notice
    ? $this->t('Review pending content')
    : '',
  '#cache' => [
    'contexts' => ['user.permissions'],
  ],
];

Use the narrowest context that correctly describes the variation. Varying by the entire user (user) can create many cache variations; user.permissions is often more appropriate if only permissions affect the output. Confirm the context’s semantics for your code and Drupal version rather than copying this example blindly.

If the text also includes a node title, the output depends on that node. A cache tag such as the node’s own cache tags should invalidate the rendered result when that node changes. When you render entities through Drupal’s normal entity view builders or add a cacheable dependency, metadata can be carried automatically. Avoid hand-maintaining tags that Drupal already knows.

Tags describe dependencies, not audiences

Suppose a block displays the title of a configurable announcement node. The announcement’s cache tags describe what to invalidate when that entity changes. If a configuration value also affects the block, its configuration dependency should be included as well. A tag is not a way to make different users see different versions; that is a context question.

Drupal’s render API bubbles cache metadata from children to parents and can affect the whole response. A child with max-age 0 can make the containing response uncacheable. That may be correct for a truly live value, but adding it as a quick fix to a stale page can eliminate useful caching far beyond the block.

Use max-age for time, not for content edits

If a notice expires at a known time, calculate the number of seconds until that deadline and use it as max-age. If the notice changes when an entity is edited, cache tags are generally a better fit than choosing an arbitrary short lifetime. Short max-age can mask missing invalidation, but it adds cache misses and still leaves a stale window.

Drupal’s documentation cautions that max-age by itself may not be sufficient in every anonymous-page scenario. Time-dependent output may need a custom cache tag invalidated by cron or another event, depending on the surrounding caching layers. Test the full response on the actual site architecture.

Debug a block that is stale or leaks a variation

  1. Reproduce with two inputs that should differ: for example, an editor and an anonymous visitor.
  2. Inspect the block’s render array and identify every value used to build its output.
  3. For each value, decide whether it affects a context, tag, or time limit.
  4. Enable render-cache debugging in a development environment or inspect cache debug headers where available.
  5. Clear caches only after changing metadata, then repeat the same two requests.
  6. Check page-level metadata, not only the block: metadata bubbles to parent render arrays and the response.

If output is correct with caches disabled but wrong with caches enabled, that narrows the investigation. It does not prove the fix is max-age: 0; trace the missing dependency and add the narrow metadata that expresses it.

Mistakes to avoid

  • Using max-age: 0 as a universal repair. It may disable caching for the full response.
  • Using a broad user context by default. It can create more variations than needed.
  • Forgetting configuration dependencies. A block can become stale even if its content entity is tagged correctly.
  • Adding tags for variation. Tags invalidate; contexts vary.
  • Testing only one account. Permission and language differences often reveal missing contexts.

For related Drupal implementation work, see when to use hook_update_N() versus hook_post_update_NAME() and how Drupal 11 object-oriented hook attributes affect migration.

My Take

Cache metadata is a description of correctness. When a block is wrong after caching, start by writing down the data it reads and the users or requests that should see a different result. That list usually makes the missing context or tag clearer than another cache clear.

Conclusion

Use contexts for variations, tags for invalidation, and max-age for time limits. Let cacheable dependencies provide metadata where possible, and verify the bubbled response with contrasting users and fresh content. A precise metadata fix preserves speed without serving the wrong output.

Sources