From 53adb72dac62e547433104678c7ea5b9c2aaa6b0 Mon Sep 17 00:00:00 2001 From: Carson Gross Date: Mon, 27 Oct 2025 13:13:52 -0600 Subject: [PATCH 1/2] add support for fetch() based refreshes --- src/htmx.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 1538b53c..689874c8 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -94,6 +94,7 @@ var htmx = (() => { viewTransitions: true, historyEnabled: true, selfRequestsOnly: true, + reloadOnHistoryNavigation: false, defaultSwapStyle: "innerHTML", defaultTimeout: 60000 /* 00 second default timeout */ } @@ -762,7 +763,10 @@ var htmx = (() => { } process(elt) { - if (this.__ignore(elt)) return + if (this.__ignore(elt)) return; + if (elt === document.body) { + this.__initializeRefreshListener(elt) + } if (elt.matches(this.__actionSelector)) { this.__initializeElement(elt) } @@ -1350,7 +1354,11 @@ var htmx = (() => { __restoreHistory(path) { path = path || location.pathname + location.search; if (this.__trigger(document.body, "htmx:before:restore:history", {path, cacheMiss: true})) { - location.reload(); + if (htmx.config.reloadOnHistoryNavigation) { + location.reload(); + } else { + this.trigger(document.body, "htmx:refresh") + } } } @@ -1645,6 +1653,26 @@ var htmx = (() => { requestQueue.abortCurrentRequest(); }) } + + __initializeRefreshListener(elt) { + if (!elt.__htmxRefreshListener) { + elt.__htmxRefreshListener = async (evt) => { + try { + let ctx = this.__createRequestContext(elt, evt); + ctx.sourceElement = elt; + ctx.sourceEvent = evt; + ctx.request.action = location.href; + ctx.request.method = "GET"; + ctx.target = "body"; + ctx.swap = "outerHTML"; + await this.handleTriggerEvent(ctx); + } catch (e) { + console.error(e) + } + } + elt.addEventListener("htmx:refresh", elt.__htmxRefreshListener) + } + } } return new Htmx() From f817eceb2777428949058a7edcfcf834ca73308c Mon Sep 17 00:00:00 2001 From: MichaelWest22 Date: Wed, 29 Oct 2025 01:52:52 +1300 Subject: [PATCH 2/2] Try moving to ajax api for history reload --- src/htmx.js | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 689874c8..1d44f6f1 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -764,9 +764,6 @@ var htmx = (() => { process(elt) { if (this.__ignore(elt)) return; - if (elt === document.body) { - this.__initializeRefreshListener(elt) - } if (elt.matches(this.__actionSelector)) { this.__initializeElement(elt) } @@ -1357,7 +1354,11 @@ var htmx = (() => { if (htmx.config.reloadOnHistoryNavigation) { location.reload(); } else { - this.trigger(document.body, "htmx:refresh") + this.ajax('GET', path, { + target: 'body', + swap: 'outerHTML', + request: {headers: {'HX-History-Restore-Request': 'true'}} + }); } } } @@ -1653,26 +1654,6 @@ var htmx = (() => { requestQueue.abortCurrentRequest(); }) } - - __initializeRefreshListener(elt) { - if (!elt.__htmxRefreshListener) { - elt.__htmxRefreshListener = async (evt) => { - try { - let ctx = this.__createRequestContext(elt, evt); - ctx.sourceElement = elt; - ctx.sourceEvent = evt; - ctx.request.action = location.href; - ctx.request.method = "GET"; - ctx.target = "body"; - ctx.swap = "outerHTML"; - await this.handleTriggerEvent(ctx); - } catch (e) { - console.error(e) - } - } - elt.addEventListener("htmx:refresh", elt.__htmxRefreshListener) - } - } } return new Htmx()