Skip to content

Refactoring: eliminate double CSS cache rebuild on theme settings save #774

@jjroelofs

Description

@jjroelofs

Summary

The CSS cache builder (dxpr_theme_css_cache_build()) runs twice on every theme settings save — once before and once after the form is processed. This is acknowledged in theme-settings.php:118 with the comment @todo come up with a less 'icky' solution.

The double execution exists as a workaround for a Drupal form processing quirk where theme settings values aren't yet available during the initial form submission handler.

Improvements

  • Faster settings save: With 20+ color schemes and extensive typography options, eliminating the redundant build halves the CSS generation time on save
  • Simpler code: Removing the workaround makes the settings save flow easier to understand and maintain
  • Reliability: The current approach creates a fragile dependency on execution order; a single post-save rebuild is more predictable

Risks

  • The replacement solution must ensure that CSS is rebuilt with the new settings values, not stale ones
  • Custom/sub-themes that override theme-settings.php may need to be aware of the change
  • Must verify that the CSS cache file is correctly written for all theme setting combinations

Involved components

  • theme-settings.php — form submit handler and cache rebuild trigger
  • dxpr_theme_callbacks.incdxpr_theme_css_cache_build() function
  • config/install/dxpr_theme.settings.yml — default settings

Optional: Intended side effects

Potential approaches:

  1. Use a #submit handler that runs after system_theme_settings_submit() to ensure new values are saved before rebuild
  2. Use a KernelEvents::TERMINATE subscriber to defer the rebuild until after the response is sent (non-blocking)
  3. Use a drupal_register_shutdown_function() callback for simplicity

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions