Fix ready() to work even when HTMX is loaded async #3398
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
This updates the
readyfunction to work correctly even if executing afterDOMContentLoadedhas been fired, but beforereadyStateis"complete". This is apparently possible when loading the script async (using theasyncattribute on the script tag.)Corresponding issue: #2329
Testing
I have a page in another project where, in about half of page-loads, HTMX fails to call the anonymous initialization function passed to
ready. Whether it happens or not depends no the exact network timing. By putting in breakpoints, I can see that in the cases where it works correctly, it's becausereadyStateis"completed". In other cases, that conditional isfalseand it adds the event listener forDOMContentLoaded. However the listener is never called. (In all the runs I've looked at,isReadyis alwaysfalse.)I verified this fix by making the same change in the copy of HTMX in that project. The page loaded correctly 10 times in a row.
I wasn't able to add new tests since I'm not really sure how to test this.
Checklist
masterfor website changes,devforsource changes)
approved via an issue
npm run test) and verified that it succeeded