Open stukennedy opened 3 months ago
Name | Link |
---|---|
Latest commit | dfba1c0271a63e3ddd66f272f0c9bbee3bdfc152 |
Latest deploy log | https://app.netlify.com/sites/htmx-extensions/deploys/668ce427feba6b00086887d1 |
Hey, to prevent any future regression and be 100% sure that the fix works as expected, could you add a test case for this ? To first reproduce the error, then ensure this fix resolves it From the top of my head, I know we can mock timeouts in the test suite (at least the main repo has some of these in its test suite), and you can configure the settle delay of htmx using its configuration property, so I would expect it to be doable!
PS: I'll shamelessly copy-paste that comment to the v1 PR on the main repo for the sake of clarity/history
Addresses the bug https://github.com/bigskysoftware/htmx/issues/2599 ... raised against the original repo.
Description
currently the head-support feature starts doing its merging based off an
htmx:afterSwap
event. This means that any script being loaded into the DOM from the htmxSwap that wants to run some initialisation code may run after thehtmx:afterHeadMerge
event has triggered, and never see it.An example for this would be if you want to return a ChartJS from an endpoint along with the script to display the chart.
The polling is necessary to check that the
Chart
class has been declared (i.e. the new head script has loaded) (and yes this could do with a timeout so it doesn't try for ever, but this is the basic solution) However, this code won't work every time if we havehtmx:afterSwap
inhead-support.js
as the trigger.Changing
htmx:afterSwap
tohtmx:afterSettle
ensures the DOM has settled (i.e. our event listener has been registered) before the head swap functionality is performed and our code works as expected.Testing
This was tested with the above code by calling this code on a lazy-loaded hx-post. The race condition seems to be gone.
Checklist
master
for website changes,dev
for source changes)npm run test
) and verified that it succeeded