diff --git a/CHANGELOG.md b/CHANGELOG.md index 1caf2289..fabc2371 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog +## [1.6.1] - 2021-11-?? + ## [1.6.0] - 2021-10-01 * Completely reworked ` + Click Me diff --git a/package.json b/package.json index 162e52fd..b9a7649a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "AJAX", "HTML" ], - "version": "1.6.0", + "version": "1.6.1", "homepage": "https://htmx.org/", "bugs": { "url": "https://github.com/bigskysoftware/htmx/issues" diff --git a/src/htmx.js b/src/htmx.js index 75d5382e..c15698b8 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -67,7 +67,7 @@ return (function () { createWebSocket: function(url){ return new WebSocket(url, []); }, - version: "1.6.0" + version: "1.6.1" }; var VERBS = ['get', 'post', 'put', 'delete', 'patch']; @@ -2481,11 +2481,13 @@ return (function () { // overriding the detail.shouldSwap property var shouldSwap = xhr.status >= 200 && xhr.status < 400 && xhr.status !== 204; var serverResponse = xhr.response; - var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse}, responseInfo); + var isError = xhr.status >= 400; + var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse, isError:isError}, responseInfo); if (!triggerEvent(target, 'htmx:beforeSwap', beforeSwapDetails)) return; target = beforeSwapDetails.target; // allow re-targeting serverResponse = beforeSwapDetails.serverResponse; // allow updating content + isError = beforeSwapDetails.isError; // allow updating error if (beforeSwapDetails.shouldSwap) { if (xhr.status === 286) { @@ -2600,7 +2602,8 @@ return (function () { } else { doSwap(); } - } else { + } + if (isError) { triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({error: "Response Status Error Code " + xhr.status + " from " + responseInfo.pathInfo.path}, responseInfo)); } } diff --git a/test/core/ajax.js b/test/core/ajax.js index d75445a5..d5f99590 100644 --- a/test/core/ajax.js +++ b/test/core/ajax.js @@ -871,7 +871,7 @@ describe("Core htmx AJAX Tests", function(){ btn.click(); this.server.respond(); btn.innerText.should.equal("Clicked!"); - htmx.off("htmx:shouldSwap", handler); + htmx.off("htmx:beforeSwap", handler); }); it('400 content can be retargeted if configured to do so', function() @@ -891,7 +891,29 @@ describe("Core htmx AJAX Tests", function(){ btn.click(); this.server.respond(); div.innerText.should.equal("Clicked!"); - htmx.off("htmx:shouldSwap", handler); + htmx.off("htmx:beforeSwap", handler); + }); + + it('errors are triggered only on 400+', function() + { + var errors = 0; + var handler = htmx.on("htmx:responseError", function(){ + errors++; + }) + this.server.respondWith("GET", "/test1", function (xhr) { + xhr.respond(204, {}, "Clicked!"); + }); + this.server.respondWith("GET", "/test2", function (xhr) { + xhr.respond(400, {}, "Clicked!"); + }); + var btn1 = make('Click Me!') + var btn2 = make('Click Me!') + btn1.click(); + btn2.click(); + this.server.respond(); + this.server.respond(); + errors.should.equal(1); + htmx.off("htmx:responseError", handler); }); @@ -908,11 +930,10 @@ describe("Core htmx AJAX Tests", function(){ xhr.respond(400, {}, "Clicked!"); }); var btn = make('Click Me!') - var div = make('') btn.click(); this.server.respond(); - div.innerText.should.equal("Clicked!!!"); - htmx.off("htmx:shouldSwap", handler); + btn.innerText.should.equal("Clicked!!!"); + htmx.off("htmx:beforeSwap", handler); }); it('scripts w/ src attribute are properly loaded', function(done) diff --git a/www/docs.md b/www/docs.md index 99181b72..3ae3cff2 100644 --- a/www/docs.md +++ b/www/docs.md @@ -103,13 +103,13 @@ It can be used via [NPM](https://www.npmjs.com/) as "`htmx.org`" or downloaded o [unpkg](https://unpkg.com/browse/htmx.org/) or your other favorite NPM-based CDN: ``` html - + ``` For added security, you can load the script using [Subresource Integrity (SRI)](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity). ``` html - + ``` ## [AJAX](#ajax) diff --git a/www/extensions/client-side-templates.md b/www/extensions/client-side-templates.md index 28f01572..b436b284 100644 --- a/www/extensions/client-side-templates.md +++ b/www/extensions/client-side-templates.md @@ -61,8 +61,8 @@ If you wish to put a template into another file, you can use a directive such as JS Bin - - + + diff --git a/www/index.md b/www/index.md index b5efdc41..2617c25e 100644 --- a/www/index.md +++ b/www/index.md @@ -35,7 +35,7 @@ By removing these arbitrary constraints htmx completes HTML as a ```html - + Click Me