Remove user.roles cache context in Comments module for better caching accuracy

Created on 21 July 2025, about 1 month ago

Problem/Motivation

In the Comment Default Formarter of the Comments module, the cache context user.roles is being added when the logic is related to permissions https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/comme....

According to Drupal's caching best practices, when rendering depends on permissions, the appropriate cache context should be user.permissions instead of user.roles.

The documentation for user.roles explicitly states that using user.permissions is preferred for any functionality that checks permissions rather than roles https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co.... In this case, after setting the user.roles cache context, the code proceeds to check a specific permission. This creates an inconsistency and may lead to incorrect cache variations.

Impact:

Increases cache fragmentation unnecessarily (since roles vary more than permissions).

May cause incorrect or stale content for users if roles change without affecting permissions.

Introduces performance overhead because of excessive cache contexts.

Proposed resolution

Remove the user.roles cache context from the Default Formatter code.

Confirm that user.permissions is already present, as this accurately reflects the dependency on permission checks.

Remaining tasks

  • Remove the user.roles cache context from the Default Formatter code.
  • Run core PHPUnit tests for Comments.
  • Verify no regressions in comment rendering.

User interface changes

None. This change affects internal caching logic only.

API changes

None. This is an internal implementation detail change.

Data model changes

None.

📌 Task
Status

Active

Version

11.0 🔥

Component

comment.module

Created by

🇪🇸Spain lpeidro Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024