Handle situation when there are no extensions that add custom swaps gracefully

This commit is contained in:
Carson Gross 2026-02-22 08:50:44 -07:00
parent b1dec6a349
commit eef2379c8b
2 changed files with 19 additions and 1 deletions

View File

@ -1323,7 +1323,7 @@ var htmx = (() => {
this.__insertNodes(parentNode, target.nextSibling, fragment);
}
} else {
let methods = this.__extMethods.get('handle_swap')
let methods = this.__extMethods.get('handle_swap') || []
let handled = false;
for (const method of methods) {
let result = method(swapStyle, target, fragment, swapSpec);

View File

@ -258,4 +258,22 @@ describe('__issueRequest unit tests', function() {
assert.isTrue(ctx.request.signal.aborted)
})
it('throws clean error for unknown swap style with no extensions', async function () {
let div = createProcessedHTML('<div hx-get="/test" hx-swap="foobar"></div>')
let ctx = htmx.__createRequestContext(div, new Event('click'))
ctx.fetch = async () => ({
status: 200,
headers: new Headers(),
text: async () => '<div>Response</div>'
})
let capturedError = null
div.addEventListener('htmx:error', (e) => capturedError = e.detail.error)
await htmx.__issueRequest(ctx)
assert.isNotNull(capturedError)
assert.include(capturedError.message, 'Unknown swap style')
})
});