Skip to content

Commit

Permalink
bug #2590 [StimulusBundle] Fix lazy load Stimulus controllers with Tu…
Browse files Browse the repository at this point in the history
…rbo (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
-------

111fd4c fix: lazy load Stimulus controllers with Turbo
  • Loading branch information
smnandre committed Feb 24, 2025
2 parents d677450 + 111fd4c commit 6d2daab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
21 changes: 8 additions & 13 deletions src/StimulusBundle/assets/dist/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
29 changes: 13 additions & 16 deletions src/StimulusBundle/assets/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 6d2daab

Please sign in to comment.