fix: head-support race condition #2599 #2704
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 have
htmx: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
forsource changes)
approved via an issue
npm run test
) and verified that it succeeded