Skip to content

Conversation

morganwahl
Copy link

@morganwahl morganwahl commented Jul 30, 2025

Description

This updates the ready function to work correctly even if executing after DOMContentLoaded has been fired, but before readyState is "complete". This is apparently possible when loading the script async (using the async attribute 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 because readyState is "completed". In other cases, that conditional is false and it adds the event listener for DOMContentLoaded. However the listener is never called. (In all the runs I've looked at, isReady is always false.)

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

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@morganwahl morganwahl changed the base branch from master to dev July 30, 2025 22:21
@morganwahl morganwahl marked this pull request as ready for review July 30, 2025 22:34
Co-authored-by: Aaron B <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants