htmx icon indicating copy to clipboard operation
htmx copied to clipboard

`htmx:afterSettle` is no longer called when returning `<title>` element within the response (2.0.0-beta1)

Open jantobola opened this issue 1 year ago • 5 comments

Expected behavior: (works in 2.0.0-alpha2)

Register 'afterSettle' event listener:

document.addEventListener('htmx:afterSettle', function(event) {
    console.log("after settle called")
});

A server returns HTML fragment:

<title>New page title</title>
<div>
  <h2>Latest posts</h2>
  <div>...</div>
</div>
  • content is swapped
  • the title is updated
  • the event is called - the message is logged into the console.

Current behavior: (2.0.0-beta1)

  • content is swapped
  • the title is updated
  • the event is NOT called - the message is NOT logged into the console.

If the <title> is not present in a response:

<div>
  <h2>Latest posts</h2>
  <div>...</div>
</div>

the event is called as expected.

UPDATE:

This happens when hx-swap is set to 'outerHTML' (https://github.com/bigskysoftware/htmx/blob/htmx-2.0/src/htmx.js#L940). This filtering causes the settleInfo.elts to be empty.

jantobola avatar Mar 20 '24 13:03 jantobola

Also noticed a regression with afterSettle and afterSwap events, which are not firing on 2.0.0 (it works straight away on 1.9.12 if I revert to it.) However, for me it is not related with having a

or not in the response.

adicco avatar Jun 21 '24 11:06 adicco

Hello, I experienced the same like adicco that afterSettle and afterSwap are not fired after update to 2.0.0 and it was fixed by going back to 1.9.12. In my application the error happens regardless of what is swapped and not only for a particular swap.

leture avatar Jul 09 '24 17:07 leture

Was this breaking change intentional? If not, is there a roadmap for fixing the behavior?

silentbugs avatar Nov 22 '24 03:11 silentbugs

Was this breaking change intentional?

Likely not @silentbugs , I haven't had the occasion to look into this yet, but bugfix PRs are always welcome if you (or anyone interested) would like to give it a shot!

Telroshan avatar Nov 22 '24 10:11 Telroshan

This seems to be fixed in version 2.0.2 (released in August). I can only reproduce this by doing all of the following:

  1. Set hx-swap="outerHTML"
  2. Include a title in the response
  3. Use version 2.0.0 or 2.0.1

scrhartley avatar Dec 16 '24 20:12 scrhartley