Skip to content

Commit 5520566

Browse files
Add historyRestoreAsHxRequest config to optionally disable hx-request header from history restore requests (#3278)
* Added Config to optionally disable the breaking HX-Request change made recently * fix broken resolved conflict
1 parent db8e5e0 commit 5520566

5 files changed

Lines changed: 107 additions & 82 deletions

File tree

src/htmx.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,14 @@ var htmx = (function() {
271271
* @type boolean
272272
* @default true
273273
*/
274-
allowNestedOobSwaps: true
274+
allowNestedOobSwaps: true,
275+
/**
276+
* Whether to treat history cache miss full page relaod requests as a "HX-Request" by returning this response header
277+
* This should always be disabled when using HX-Request header to optionally return partial responses
278+
* @type boolean
279+
* @default true
280+
*/
281+
historyRestoreAsHxRequest: true
275282
},
276283
/** @type {typeof parseInterval} */
277284
parseInterval: null,
@@ -3193,7 +3200,9 @@ var htmx = (function() {
31933200
const details = { path, xhr: request }
31943201
triggerEvent(getDocument().body, 'htmx:historyCacheMiss', details)
31953202
request.open('GET', path, true)
3196-
request.setRequestHeader('HX-Request', 'true')
3203+
if (htmx.config.historyRestoreAsHxRequest) {
3204+
request.setRequestHeader('HX-Request', 'true')
3205+
}
31973206
request.setRequestHeader('HX-History-Restore-Request', 'true')
31983207
request.setRequestHeader('HX-Current-URL', location.href)
31993208
request.onload = function() {

test/core/headers.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ describe('Core htmx AJAX headers', function() {
401401
htmx.location = window.location
402402
})
403403

404-
it('request to restore history should include the HX-Request header', function() {
404+
it('request to restore history should include the HX-Request header when historyRestoreAsHxRequest true', function() {
405405
this.server.respondWith('GET', '/test', function(xhr) {
406406
xhr.requestHeaders['HX-Request'].should.be.equal('true')
407407
xhr.respond(200, {}, '')
@@ -410,6 +410,17 @@ describe('Core htmx AJAX headers', function() {
410410
this.server.respond()
411411
})
412412

413+
it('request to restore history should not include the HX-Request header when historyRestoreAsHxRequest false', function() {
414+
htmx.config.historyRestoreAsHxRequest = false
415+
this.server.respondWith('GET', '/test', function(xhr) {
416+
should.equal(xhr.requestHeaders['HX-Request'], undefined)
417+
xhr.respond(200, {}, '')
418+
})
419+
htmx._('loadHistoryFromServer')('/test')
420+
this.server.respond()
421+
htmx.config.historyRestoreAsHxRequest = true
422+
})
423+
413424
it('request history from server with error status code throws error event', function() {
414425
this.server.respondWith('GET', '/test', function(xhr) {
415426
xhr.requestHeaders['HX-Request'].should.be.equal('true')

www/content/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ Note that using a [meta tag](@/docs.md#config) is the preferred mechanism for se
137137
* `scrollIntoViewOnBoost:true` - boolean: whether or not the target of a boosted element is scrolled into the viewport. If `hx-target` is omitted on a boosted element, the target defaults to `body`, causing the page to scroll to the top.
138138
* `triggerSpecsCache:null` - object: the cache to store evaluated trigger specifications into, improving parsing performance at the cost of more memory usage. You may define a simple object to use a never-clearing cache, or implement your own system using a [proxy object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
139139
* `htmx.config.responseHandling:[...]` - HtmxResponseHandlingConfig[]: the default [Response Handling](@/docs.md#response-handling) behavior for response status codes can be configured here to either swap or error
140-
* `htmx.config.allowNestedOobSwaps:true` - boolean: whether to process OOB swaps on elements that are nested within the main response element. See [Nested OOB Swaps](@/attributes/hx-swap-oob.md#nested-oob-swaps).
140+
* `htmx.config.allowNestedOobSwaps:true` - boolean: whether to process OOB swaps on elements that are nested within the main response element. See [Nested OOB Swaps](@/attributes/hx-swap-oob.md#nested-oob-swaps).
141+
* `htmx.config.historyRestoreAsHxRequest:true` - Whether to treat history cache miss full page relaod requests as a "HX-Request" by returning this response header. This should always be disabled when using HX-Request header to optionally return partial responses
141142

142143
##### Example
143144

0 commit comments

Comments
 (0)