Skip to content

Conversation

@josbeir
Copy link
Contributor

@josbeir josbeir commented Sep 13, 2025

This PR makes the toolbar work when used with libraries (such as HTMX, Hotwire, etc.) that swap or
replace the <body> element or perform partial page updates.

Improvements

  • The toolbar is now kept outside of the documet.body, this keeps it from being removed, even if the body element is swapped.

The main error i experienced was

Uncaught TypeError: can't access property "postMessage", iframe.contentWindow is null

This error occured after transitioning to another page (e.g., with htmx:boost or similar techniques) because the toolbar iframe was lost or not properly re-injected.

Copilot AI review requested due to automatic review settings September 13, 2025 13:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the DebugKit toolbar to work reliably with JavaScript frameworks like HTMX and Hotwire that perform partial page updates or DOM swaps. The changes address an issue where the toolbar iframe would be lost after navigation, causing "iframe.contentWindow is null" errors.

  • Implements MutationObserver to automatically re-inject the toolbar iframe when it's removed from the DOM
  • Updates the toolbar iframe's src with the latest debug kit ID from AJAX responses
  • Prevents duplicate event listeners and XMLHttpRequest proxying to avoid memory leaks

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@josbeir josbeir changed the title Make DebugKit toolbar robust with JS frameworks like HTMX, Boost Make DebugKit toolbar robust with JS frameworks like HTMX, Hotwire, ... Sep 13, 2025
@josbeir
Copy link
Contributor Author

josbeir commented Sep 13, 2025

This is now ready for testing / comments :-)

@markstory markstory added this to the 5.x milestone Sep 13, 2025
- Persist toolbar iframe by placing it in document.documentElement instead of document.body.
- Handle potential error by logging it to the console
- Cleanups
@josbeir
Copy link
Contributor Author

josbeir commented Sep 13, 2025

I changed some things, realizing I overcomplicated it by adding the iframe to the shadow root, which I then moved outside document.body. The iframe alone outside of document.body is enough here.

@markstory
Copy link
Member

The iframe alone outside of document.body is enough here.

Is that a valid location for iframes? I tried reading through the HTML spec and iframes are part of 'embedded content' and I'm not sure that iframes can be under the html element.

Another solution could be to have debugkit render for these requests. We could expose it as a setting. I don't think we can start appending debugkit to all HTML responses, without an opt-in.

@josbeir
Copy link
Contributor Author

josbeir commented Sep 17, 2025

The iframe alone outside of document.body is enough here.

Is that a valid location for iframes? I tried reading through the HTML spec and iframes are part of 'embedded content' and I'm not sure that iframes can be under the html element.

Browsers are pretty forgiving about this but after some extra research it looks like this is indeed not really valid if you follow the html spec stricly. You can therefore currently ignore this MR.

Another solution could be to have debugkit render for these requests. We could expose it as a setting. I don't think we can start appending debugkit to all HTML responses, without an opt-in.

Yeah i think a more dynamic approach would be better anyway. I will close this MR and do some thinking for a cleaner solution.

@josbeir josbeir closed this Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants