Skip to content

fix: HX-Trigger with null event detail no longer throws - #4136

Open
Lluczel wants to merge 1 commit into
bigskysoftware:four-devfrom
Lluczel:fix/4135-hx-trigger-null-detail
Open

Lluczel wants to merge 1 commit into
bigskysoftware:four-devfrom
Lluczel:fix/4135-hx-trigger-null-detail

Conversation

@Lluczel

@Lluczel Lluczel commented Oct 10, 2026

Copy link
Copy Markdown

Description

An HX-Trigger response header whose event detail is null, e.g. HX-Trigger: {"myEvent": null}, throws TypeError: Cannot read properties of null (reading 'cancelled'), and the event is never dispatched.

__handleTriggerHeader decides whether to pass the detail through as-is with typeof detail === 'object'. Since typeof null === 'object', null is passed straight to trigger(). Its detail = {} default only applies to undefined, so !detail.cancelled throws.

This change excludes null from the pass-through branch, so it is wrapped like any other non-object value ({value: null}), the same way {"myEvent": 42} becomes {value: 42}. Object details and the comma-separated string form are unchanged.

The issue suggests a guard in trigger() instead. I kept the fix where the header is parsed for two reasons: a header-supplied null then follows the existing rule for non-object details (handlers read event.detail.value), and the public trigger() API stays untouched. Happy to add a guard there as well if you'd prefer.

HX-Trigger: {"myEvent": null}

Corresponding issue: #4135

Closes #4135

Testing

  • Added wraps null detail in value property to test/tests/unit/__handleTriggerHeader.js. It fails on four-dev with the TypeError above and passes with this change.
  • npm test (Chromium): 1781 passed, 0 failed, 4 skipped.
  • python3 src/scripts/content/check.py: OK.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes) (targeting four-dev, since this is an htmx 4 source fix)
  • 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

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.

1 participant