Skip to content

Conversation

@maximebf
Copy link

Description

I'm working on implementing streaming responses in my app similarly to React Suspense. The main body of the page is returned first and then blocks of content are streamed and replaced in the page.

This method works great in practice but HTMX is not compatible with it because it waits on the DOMContentLoaded event to initialize itself. However, using this technique, the event only occurs once the request is finished. This is a problem as the user has a fully rendered page in front of his eyes, than can already be interacted with but HTMX is still not loaded.

My propose change exposes an htmx.bootstrap() function that allows to manually bootstrap htmx at a choosen time. The function can be safely called multiple times but it will only execute once. It will automatically trigger on DOMContentLoaded like it does today.

The change is minimal but allows greater control, needed for the streaming use case.

Testing

Tested manually + ran "npm run test"
Minimal change.

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

@MichaelWest22
Copy link
Collaborator

I think this could be a great change to resolve the loading issue htmx has always had with edge cases. see #2264 and the many linked issues and PR's we have tried in the past! The last proposal put forward was to implement a initialize() function so it could be good to rename your implementation to that maybe if it makes sense. don't think .init() would be good as it is already used in extensions etc.

We have to make sure we don't cause any regressions or issues for existing users but I think your proposed change should be fully backwards compatible.

The biggest issue is how to test and validate this solution is working? are you able to produce the most minimal html example that can show it is working as expected we can add to our manual tests?

master...MichaelWest22:initialize

Here is a quick stab at what other things might be needed you can borrow the good bits from.

@1cg
Copy link
Contributor

1cg commented Oct 17, 2025

@maximebf I love this change! However, I would prefer the method be called initialize() (like in @MichaelWest22's change) and I would also like to see a docs update as well as a manual test if at all possible to ensure that the functionality works. I know this is almost impossible to test in our automated suite. Thank you!

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.

3 participants