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

Bug? hx-on:* not fired when using hx-trigger="load" #3106

Open
michal-laskowski opened this issue Jan 5, 2025 · 1 comment
Open

Bug? hx-on:* not fired when using hx-trigger="load" #3106

michal-laskowski opened this issue Jan 5, 2025 · 1 comment
Labels
bug Something isn't working help wanted We would be interested in a PR that resolves that issue

Comments

@michal-laskowski
Copy link

This will not work

  it('before-request - without delay', function() {
    var requests = 0
    window.foo = 0
    this.server.respondWith('GET', '/test', function(xhr) {
      requests++
      xhr.respond(200, {}, 'Requests: ' + requests)
    })
    var div = make('<div hx-trigger="load, click" hx-get="/test" hx-on:htmx:before-request="window.foo += 1">Requests: 0</div>')
    div.innerHTML.should.equal('Requests: 0')
    this.server.respond()
    div.innerHTML.should.equal('Requests: 1')
    div.click()
    this.server.respond()
    div.innerHTML.should.equal('Requests: 2')
    delete window.foo
    window.foo.should.equal(2)
  })

When usind delay:1 it will work.

  it('before-request - with delay', function(done) {
    var requests = 0
    window.foo = 0
    this.server.respondWith('GET', '/test', function(xhr) {
      requests++
      xhr.respond(200, {}, 'Requests: ' + requests)
    })
    var div = make('<div hx-trigger="load delay:1ms" hx-get="/test" hx-on:htmx:before-request="window.foo += 1">Requests: 0</div>')
    div.innerHTML.should.equal('Requests: 0')
    this.server.autoRespond = true
    this.server.autoRespondAfter = 5

    setTimeout(function() {
      window.foo.should.equal(1)
      delete window.foo
      div.innerHTML.should.equal('Requests: 1')
      done()
    }, 50)
  })
@Telroshan Telroshan added bug Something isn't working help wanted We would be interested in a PR that resolves that issue labels Jan 8, 2025
@rkilpadi
Copy link
Contributor

rkilpadi commented Jan 13, 2025

I found similar issues with reveal and intersect. Looks like this bug happens when triggers fire before the node is fully processed (see addTriggerHandler()). hx-on attributes are processed after the rest of the node (see processNode()) and therefore don't have a chance to attach before these triggers fire.

Edit: Nevermind this next part, I can't get inheritance to work on triggers at all, if this is a bug it is a separate issue.

I also found issues with inheritance for all these triggers when invoking hx-on attributes.
The following works as intended and increments foo only when the div is not immediately revealed:

<div hx-trigger="revealed" hx-get="/test" hx-on::before-request="foo++"></div>

However, the following doesn't increment foo in either case:

<div hx-trigger="revealed">
    <div hx-get="/test" hx-on::before-request="foo++"></div>
</div>

Replacing revealed with load delay:1ms has similar results.

I wonder if hx-on processing logic could/should be rewritten so that it can happen within initNode() to prevent some of these issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted We would be interested in a PR that resolves that issue
Projects
None yet
Development

No branches or pull requests

3 participants