diff --git a/assets/hx.js b/assets/hx.js new file mode 100644 index 0000000..be1ac09 --- /dev/null +++ b/assets/hx.js @@ -0,0 +1,30 @@ +// Site-wide htmx policy. Loaded right after htmx itself. +(function () { + // htmx wires up swapped-in content (e.g. a dialog's hx-post form) in its + // settle step, by default 20ms after the swap: a click or Enter in that + // window would submit the form natively. No settle transitions are used, + // so settle in the same tick. + htmx.config.defaultSettleDelay = 0; + + // A server-sent HX-Redirect follows a mutation (move, delete, …): load the + // target into the current history entry rather than pushing a new one, so + // Back skips the stale pre-mutation page. replaceState + reload rather than + // location.replace: a target differing only by #fragment would otherwise + // not re-fetch. + htmx.location = { + set href(target) { + window.history.replaceState(null, '', target); + window.location.reload(); + }, + reload: function () { window.location.reload(); } + }; + + // Failed requests answer with a plain-text reason; show it. + document.addEventListener('htmx:responseError', function (e) { + var xhr = e.detail.xhr; + alert(xhr.responseText || ('Request failed (' + xhr.status + ')')); + }); + document.addEventListener('htmx:sendError', function () { + alert('Network error'); + }); +})(); diff --git a/assets/layout.html b/assets/layout.html index f8bbe1f..29ceb4f 100644 --- a/assets/layout.html +++ b/assets/layout.html @@ -8,6 +8,8 @@ + + @@ -77,6 +79,7 @@ {{if not .EditMode}}{{block "aside" .}}{{end}}{{end}} {{block "extras" .}}{{end}} +