fix: head-support race condition#45
Open
stukennedy wants to merge 1 commit intobigskysoftware:mainfrom
Open
Conversation
✅ Deploy Preview for htmx-extensions canceled.
|
4 tasks
Collaborator
|
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 PS: I'll shamelessly copy-paste that comment to the v1 PR on the main repo for the sake of clarity/history |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Addresses the bug bigskysoftware/htmx#2599 ... raised against the original repo.
Description
currently the head-support feature starts doing its merging based off an
htmx:afterSwapevent. This means that any script being loaded into the DOM from the htmxSwap that wants to run some initialisation code may run after thehtmx:afterHeadMergeevent 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
Chartclass 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:afterSwapinhead-support.jsas the trigger.Changing
htmx:afterSwaptohtmx:afterSettleensures 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
masterfor website changes,devforsource changes)
approved via an issue
npm run test) and verified that it succeeded