bug #2590 [StimulusBundle] Fix lazy load Stimulus controllers with Turbo (smnandre)

This PR was merged into the 2.x branch.

Discussion
----------

[StimulusBundle] Fix  lazy load Stimulus controllers with Turbo

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Issues        | Fix #2576 / maybe #2583
| License       | MIT

Partial revert of some changes to fix issues with Turbo and lazyload Stimulus controllers

Commits
-------

111fd4cda85 fix: lazy load Stimulus controllers with Turbo
This commit is contained in:
Simon André
2025-02-24 23:50:41 +01:00
2 changed files with 21 additions and 29 deletions

21
assets/dist/loader.js vendored
View File

@@ -53,22 +53,17 @@ class StimulusLazyControllerHandler {
return;
}
new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
switch (mutation.type) {
case 'childList': {
for (const node of mutation.addedNodes) {
if (node instanceof Element) {
extractControllerNamesFrom(node).forEach((controllerName) => {
this.loadLazyController(controllerName);
});
}
for (const { attributeName, target, type } of mutationsList) {
switch (type) {
case 'attributes': {
if (attributeName === controllerAttribute &&
target.getAttribute(controllerAttribute)) {
extractControllerNamesFrom(target).forEach((controllerName) => this.loadLazyController(controllerName));
}
break;
}
case 'attributes': {
if (mutation.attributeName === controllerAttribute) {
extractControllerNamesFrom(mutation.target).forEach((controllerName) => this.loadLazyController(controllerName));
}
case 'childList': {
this.lazyLoadExistingControllers(target);
}
}
}

View File

@@ -99,26 +99,23 @@ class StimulusLazyControllerHandler {
return;
}
new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
switch (mutation.type) {
case 'childList': {
// @ts-ignore
for (const node of mutation.addedNodes) {
if (node instanceof Element) {
extractControllerNamesFrom(node).forEach((controllerName) => {
this.loadLazyController(controllerName);
});
}
}
break;
}
for (const { attributeName, target, type } of mutationsList) {
switch (type) {
case 'attributes': {
if (mutation.attributeName === controllerAttribute) {
extractControllerNamesFrom(mutation.target as Element).forEach((controllerName) =>
if (
attributeName === controllerAttribute &&
(target as Element).getAttribute(controllerAttribute)
) {
extractControllerNamesFrom(target as Element).forEach((controllerName) =>
this.loadLazyController(controllerName)
);
}
break;
}
case 'childList': {
this.lazyLoadExistingControllers(target as Element);
}
}
}