Problem/Motivation
When swapping out the body element, then Drupal behaviors are not applied by core/drupal.htmx library's htmx-behaviors.js.
The file htmx-behaviors.js contains following event listener:
htmx.on('htmx:drupal:load', ({ detail }) => {
attachFromHtmx = true;
Drupal.attachBehaviors(detail.elt, drupalSettings);
attachFromHtmx = false;
});
It's being triggered by the logic within htmx-assets.js (also part of core/drupal.htmx):
htmx.on('htmx:afterSettle', ({ detail }) => {
requestAssetsLoaded.get(detail.xhr).then(() => {
// Some HTMX swaps put the incoming element before or after detail.elt.
htmx.trigger(detail.elt.parentNode, 'htmx:drupal:load');
// This should be automatic but don't wait for the garbage collector.
requestAssetsLoaded.delete(detail.xhr);
});
});
This logic does not trigger the event listener when the element being swapped is the <body> element.
Steps to reproduce
Add hx-boost="true" as an attribute to the <body> element and add core/drupal.htmx library via template_preprocess_html.
Proposed resolution
This part of the logic within htmx-assets.js passes along the parent element:
htmx.trigger(detail.elt.parentNode, 'htmx:drupal:load');
When doing this instead:
if (detail.elt.tagName === 'BODY') {
htmx.trigger(detail.elt, 'htmx:drupal:load');
}
else {
htmx.trigger(detail.elt.parentNode, 'htmx:drupal:load');
}
Then the event listener is triggered and behaviors are attached as expected. So maybe we need to handle the body element as some sort of exception at this place?
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3560659
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
nod_Makes sense, htmx does have an exception for the body when using outerHTML, we could do that too. I'm not super happy with how we figure out what is the element passed to attachbehaviors but I don't have better to propose.
Comment #3
fathershawnComment #4
quietone commentedJust adjusting version.
Comment #8
fathershawnTests passing. Test only branch provided as Nightwatch tests are not run by the test only job, as far as I know.
Comment #9
godotislateCouple questions on the MR. Otherwise changes look pretty straightforward. Nice work!
Nightwatch test fails as expected on test-only MR: https://git.drupalcode.org/issue/drupal-3560659/-/jobs/8259106
Comment #10
godotislateThink this looks good now.
Comment #11
longwaveBackported to 11.3.x as a low risk eligible bug fix.
Committed and pushed 07d426b4075 to main and cf25b3cd1b7 to 11.x and 25e70b1b59b to 11.3.x. Thanks!
Comment #17
mxh commentedHey, this is great news. Thank you guys so much for all the work to fix this.