bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.56k stars 1.31k forks source link

Do not execute hx-trigger="load" on re-initialization of an existing node #2976

Open basvk opened 1 month ago

basvk commented 1 month ago

Description

Sometimes, requests that should be only triggered once at load (with hx-trigger="load") are executed twice. This happens for example when calling htmx.process on an existing node (or a parent node) after changing an attribute on the element with hx-trigger="load". This PR adds a firstInitCompleted flag to an element's internalData and adds a check for that flag when handling the load triggerEvent.

Corresponding issue:

2973

Testing

Added a test scenario that changes hx-swap="afterbegin" to hx-swap="beforeend" on an element with hx-trigger="load", followed by a call to htmx.process and checks whether the innerHTML of the element stayed the same. This test fails on the dev branch, and passes with this fix.

Checklist

basvk commented 1 month ago

Thanks, good idea! I've changed firstInit into firstInitCompleted and removed that unnecessary check in initNode.