Skip to content

[StimulusBundle] Conditional Loading of Stimulus Controllers Based on Configuration #2321

Open
@Jamie-Vandenbalck

Description

@Jamie-Vandenbalck

Background

I am currently working on a Symfony application that has distinct sections for customers and administrators. Each section has its own set of Stimulus controllers. The problem I face is that when I configure controllers to be eagerly loaded, all controllers are loaded across both the customer and admin sides. This leads to unnecessary loading and potential performance issues.

Current Situation

The Symfony StimulusBundle loads controllers either eagerly or lazily, but this is configured globally or through static annotations (/* stimulusFetch: eager */). There is currently no built-in mechanism to conditionally load controllers based on configuration or environment.

Problem Statement

In my application:

  1. I have controllers specific to the customer side and controllers specific to the admin side.
  2. I do not want customer-side controllers to load eagerly in the admin interface and vice versa.
  3. Managing controller loading globally results in increased load times and resource usage, as unnecessary controllers are loaded.

Proposed Feature

I propose adding a feature to the StimulusBundle that allows splitting of Stimulus controllers based on configuration settings. This would enable developers to specify which controllers should be loaded in different contexts (e.g., customer or admin).

Suggested Implementation

  1. Configuration Key:
  • Add a configuration option in stimulus.yaml to specify different groups of controllers.
  • Example:
# config/packages/stimulus.yaml
stimulus:
  controller_paths:
    app:
      - '%kernel.project_dir%/assets/controllers/app'
    admin:
      - '%kernel.project_dir%/assets/controllers/admin'
  1. StimulusBundle Logic:
  • Modify the StimulusBundle to read from the configuration and conditionally register controllers based on the context or environment.
  • Implement logic to detect the current context and load the appropriate set of controllers.
// assets/bootstrap.js

import { startStimulusApp } from '@symfony/stimulus-bundle';

const app = startStimulusApp('app');

Benefits

  1. Performance Optimization: Only the necessary controllers are loaded for the given context, reducing load times and resource usage.
  2. Improved Maintainability: Easier to manage controllers and configurations for different parts of the application.
  3. Flexibility: Developers can configure which controllers should be loaded eagerly or lazily based on their application’s needs.

Example Use Case

  1. A Symfony application with a customer-facing site and an admin dashboard.
  2. The customer side only needs controllers like home_controller and product_controller.
  3. The admin side requires controllers like dashboard_controller and user_management_controller.
  4. With this feature, developers could specify eager loading only for the relevant controllers for each side, avoiding cross-loading and maintaining a lean asset load.

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