Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

head-support extension fails if another extension calls htmx.swap() directly #120

Open
mkgn opened this issue Nov 26, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@mkgn
Copy link

mkgn commented Nov 26, 2024

In one of my extensions I use internal method htmx.swap(). I also use head support extension as well. head-support extension has below code where it tap into htmx:afterSwap. It try to access the evt.detail.xhr and it's null in my case because I used htmx.swap() inside my extension.. I think we should execute head-support only if it has a valid xhr responce. a simple null check would do?

    htmx.defineExtension("head-support", {
        init: function(apiRef) {
            // store a reference to the internal API.
            api = apiRef;

            htmx.on('htmx:afterSwap', function(evt){
                var serverResponse = evt.detail.xhr.response;
                if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
                    mergeHead(serverResponse, evt.detail.boosted ? "merge" : "append");
                }
            })

            htmx.on('htmx:historyRestore', function(evt){
                if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
                    if (evt.detail.cacheMiss) {
                        mergeHead(evt.detail.serverResponse, "merge");
                    } else {
                        mergeHead(evt.detail.item.head, "merge");
                    }
                }
            })

            htmx.on('htmx:historyItemCreated', function(evt){
                var historyItem = evt.detail.item;
                historyItem.head = document.head.outerHTML;
            })
        }
    });
@MichaelWest22
Copy link
Contributor

@Telroshan is this best moved to https://github.com/bigskysoftware/htmx-extensions/issues?

I think the fix would need to be done to the head-support extension like

            htmx.on('htmx:afterSwap', function(evt){
                if (evt.detail.xhr) {
                  var serverResponse = evt.detail.xhr.response;
                  if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
                      mergeHead(serverResponse, evt.detail.boosted ? "merge" : "append");
                  }
                }
            })

So that this event only triggers for ajax request swaps.

@Telroshan Telroshan transferred this issue from bigskysoftware/htmx Dec 6, 2024
@Telroshan
Copy link
Collaborator

Sure, a null check sounds good to me.
Feel free to open a bugfix PR if you're interested!

@Telroshan Telroshan added the bug Something isn't working label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants