Skip to content

Replace drupal_flush_all_caches() with targeted invalidation#829

Merged
jjroelofs merged 3 commits into
8.xfrom
jur/8.x/826-targeted-cache-invalidation
Jul 8, 2026
Merged

Replace drupal_flush_all_caches() with targeted invalidation#829
jjroelofs merged 3 commits into
8.xfrom
jur/8.x/826-targeted-cache-invalidation

Conversation

@jjroelofs

Copy link
Copy Markdown
Collaborator

Linked issues

Solution

Replace drupal_flush_all_caches() with targeted cache invalidation that only clears CSS/JS aggregates, render cache, and library discovery cache. This avoids rebuilding the entire service container and router when only a CSS file has changed.

Checklist

  • I have read the CONTRIBUTING.md document.
  • My commit messages follow the contributing standards and style of this project.
  • My code follows the coding standards and style of this project.
  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to change).
  • Need to run update.php after code changes.
  • Requires a change to end-user documentation.
  • Requires a change to developer documentation.
  • Requires a change to QA tests.
  • Requires a new QA test.
  • I have updated the documentation accordingly.
  • All new and existing tests passed.

Avoid rebuilding the entire service container and router when only
theme CSS has changed. Instead, clear only CSS/JS aggregates, the
render cache, and the library discovery cache.

Fix #826
@jjroelofs

Copy link
Copy Markdown
Collaborator Author

Review findings:

  • [P1] dxpr_theme_callbacks.inc:99: \Drupal::cache('render')->invalidateAll() only invalidates the render cache bin. It does not invalidate Drupal's rendered cache tag, so Internal Page Cache/Dynamic Page Cache can keep serving cached HTML with old CSS asset links, especially from post-update rebuilds that do not change theme config. Prefer \\Drupal\\Core\\Cache\\Cache::invalidateTags(['rendered']).\n\n- [P2] dxpr_theme_callbacks.inc:95: the replacement no longer resets asset.query_string. When aggregation is disabled, the generated themesettings-$theme.css URL remains the same, so browsers/proxies can keep the old CSS after the file is overwritten. Add \\Drupal::service('asset.query_string')->reset().\n\n- [P2] dxpr_theme_callbacks.inc:103: library.discovery->clearCachedDefinitions() is deprecated in Drupal 11.1, while this theme declares ^11 support. Use clear() when available, falling back to clearCachedDefinitions() for Drupal 9/10 compatibility.\n\nChecked GitHub CI: green. Also ran php -l on the PR version of dxpr_theme_callbacks.inc; no syntax errors.

@jjroelofs jjroelofs left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for narrowing the cache clear. I found two stale-cache paths that the old full flush covered and the targeted version currently misses, so I think this needs one more pass before merge.

Comment thread dxpr_theme_callbacks.inc
// If the CSS & JS aggregation are enabled we need to clear the caches.
drupal_flush_all_caches();
// Clear CSS/JS aggregated files.
\Drupal::service('asset.css.collection_optimizer')->deleteAll();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This targeted replacement drops the CSS/JS asset query-string reset that drupal_flush_all_caches() performed (_drupal_flush_css_js() on Drupal 9/early 10, asset.query_string->reset() on newer core). When aggregation is disabled, themesettings-*.css keeps the same URL/query suffix after the file is overwritten, so browsers or edge caches can keep serving the old CSS. Please reset the asset query string as part of this helper, with a fallback for the supported Drupal 9.3/10 range.

Comment thread dxpr_theme_callbacks.inc
\Drupal::service('asset.css.collection_optimizer')->deleteAll();
\Drupal::service('asset.js.collection_optimizer')->deleteAll();
// Clear the render cache so pages pick up the new CSS.
\Drupal::cache('render')->invalidateAll();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalidating only the render bin still leaves full page/dynamic page cache responses that can contain the old asset URL or old query string. The previous full cache flush cleared those persistent bins, so anonymous users may continue to get stale CSS until those entries expire. Please also invalidate the relevant response caches/tags, or ensure the generated theme-settings CSS is represented by a cache tag that reaches cached page responses.

@jjroelofs jjroelofs left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up. The asset query-string reset and page-cache invalidation concerns from the earlier review look addressed in 517dd92. I found one remaining Drupal 11 compatibility issue below.

Comment thread dxpr_theme_callbacks.inc Outdated
// Clear the discovery cache for library definitions.
\Drupal::cache('discovery')->invalidateAll();
// Invalidate the library info cache.
\Drupal::service('library.discovery')->clearCachedDefinitions();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still calls clearCachedDefinitions(), which is deprecated in Drupal 11.1 while this theme declares ^11 support. Please use clear() when the service provides it, falling back to clearCachedDefinitions() for Drupal 9/10 compatibility, e.g. store \Drupal::service('library.discovery') in a variable and branch with method_exists($library_discovery, 'clear').

clearCachedDefinitions() is deprecated in Drupal 11.1. Use clear()
when available, falling back for Drupal 9/10 compatibility.
@jjroelofs
jjroelofs merged commit fa067b0 into 8.x Jul 8, 2026
5 checks passed
@jjroelofs
jjroelofs deleted the jur/8.x/826-targeted-cache-invalidation branch July 8, 2026 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactoring: replace drupal_flush_all_caches() with targeted invalidation

1 participant