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

Do not execute hx-trigger="load" on re-initialization of an existing node #2976

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

basvk
Copy link

@basvk basvk commented Oct 20, 2024

Description

Sometimes, requests that should be only triggered once at load (with hx-trigger="load") are executed twice. This happens for example when calling htmx.process on an existing node (or a parent node) after changing an attribute on the element with hx-trigger="load".
This PR adds a firstInitCompleted flag to an element's internalData and adds a check for that flag when handling the load triggerEvent.

Corresponding issue:
#2973

Testing

Added a test scenario that changes hx-swap="afterbegin" to hx-swap="beforeend" on an element with hx-trigger="load", followed by a call to htmx.process and checks whether the innerHTML of the element stayed the same.
This test fails on the dev branch, and passes with this fix.

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

@basvk basvk changed the base branch from master to dev October 20, 2024 14:16
Copy link
Collaborator

@Telroshan Telroshan left a comment

Choose a reason for hiding this comment

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

Just an idea out loud, not a change request, what would you think of "reversing" the check to simplify the code, i.e. only process the load trigger if the nodeData.something property is falsy, and only set that property to true after initialization ?
This would remove the need for that first true assignment in initNode as well as the undefined comparison, as a falsy check would cover it (so only assign it to true after initialization, and check that it's falsy in addTriggerHandler)

@Telroshan Telroshan added the bug Something isn't working label Oct 21, 2024
@basvk
Copy link
Author

basvk commented Oct 21, 2024

Thanks, good idea! I've changed firstInit into firstInitCompleted and removed that unnecessary check in initNode.

@Telroshan Telroshan added the ready for review Issues that are ready to be considered for merging label Oct 22, 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 ready for review Issues that are ready to be considered for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants