Skip to content

Commit a706897

Browse files
fix hx-location to handle replace (#3741)
1 parent 1e80780 commit a706897

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/htmx.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4696,8 +4696,10 @@ var htmx = (function() {
46964696
const requestPath = responseInfo.pathInfo.finalRequestPath
46974697
const responsePath = responseInfo.pathInfo.responsePath
46984698

4699-
const pushUrl = responseInfo.etc.push || getClosestAttributeValue(elt, 'hx-push-url')
4700-
const replaceUrl = responseInfo.etc.replace || getClosestAttributeValue(elt, 'hx-replace-url')
4699+
let pushUrl = responseInfo.etc.push || getClosestAttributeValue(elt, 'hx-push-url')
4700+
let replaceUrl = responseInfo.etc.replace || getClosestAttributeValue(elt, 'hx-replace-url')
4701+
if (pushUrl === 'false') pushUrl = null
4702+
if (replaceUrl === 'false') replaceUrl = null
47014703
const elementIsBoosted = getInternalData(elt).boosted
47024704

47034705
let saveType = null
@@ -4715,11 +4717,6 @@ var htmx = (function() {
47154717
}
47164718

47174719
if (path) {
4718-
// false indicates no push, return empty object
4719-
if (path === 'false') {
4720-
return {}
4721-
}
4722-
47234720
// true indicates we want to follow wherever the server ended up sending us
47244721
if (path === 'true') {
47254722
path = responsePath || requestPath // if there is no response path, go with the original request path
@@ -4825,7 +4822,7 @@ var htmx = (function() {
48254822
redirectPath = redirectSwapSpec.path
48264823
delete redirectSwapSpec.path
48274824
}
4828-
redirectSwapSpec.push = redirectSwapSpec.push || 'true'
4825+
redirectSwapSpec.push = redirectSwapSpec.push ?? 'true'
48294826
ajaxHelper('get', redirectPath, redirectSwapSpec)
48304827
return
48314828
}

www/static/test/core/headers.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,38 @@ describe('Core htmx AJAX headers', function() {
442442
}, 30)
443443
})
444444

445+
it('should replace Url on HX-Location if push is false and replace is set', function(done) {
446+
sessionStorage.setItem('htmx-current-path-for-history', '/old')
447+
this.server.respondWith('GET', '/test', [200, { 'HX-Location': '{"push":false, "replace":"true", "path":"/test2", "target":"#work-area"}' }, ''])
448+
this.server.respondWith('GET', '/test2', [200, {}, '<div>Yay! Welcome</div>'])
449+
var div = make('<div id="testdiv" hx-trigger="click" hx-get="/test"></div>')
450+
div.click()
451+
this.server.respond()
452+
this.server.respond()
453+
setTimeout(function() {
454+
getWorkArea().innerHTML.should.equal('<div>Yay! Welcome</div>')
455+
var path = sessionStorage.getItem('htmx-current-path-for-history')
456+
path.should.equal('/test2')
457+
done()
458+
}, 30)
459+
})
460+
461+
it('should replace Url on HX-Location if push is string false and replace is set', function(done) {
462+
sessionStorage.setItem('htmx-current-path-for-history', '/old')
463+
this.server.respondWith('GET', '/test', [200, { 'HX-Location': '{"push":"false", "replace":"true", "path":"/test2", "target":"#work-area"}' }, ''])
464+
this.server.respondWith('GET', '/test2', [200, {}, '<div>Yay! Welcome</div>'])
465+
var div = make('<div id="testdiv" hx-trigger="click" hx-get="/test"></div>')
466+
div.click()
467+
this.server.respond()
468+
this.server.respond()
469+
setTimeout(function() {
470+
getWorkArea().innerHTML.should.equal('<div>Yay! Welcome</div>')
471+
var path = sessionStorage.getItem('htmx-current-path-for-history')
472+
path.should.equal('/test2')
473+
done()
474+
}, 30)
475+
})
476+
445477
it('should push different Url on HX-Location if push Url is string', function(done) {
446478
sessionStorage.removeItem('htmx-current-path-for-history')
447479
var HTMX_HISTORY_CACHE_NAME = 'htmx-history-cache'

0 commit comments

Comments
 (0)