Skip to content

Commit c7fba53

Browse files
committed
Add replace option to api as well
1 parent c6a0c19 commit c7fba53

5 files changed

Lines changed: 73 additions & 81 deletions

File tree

src/htmx.js

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,7 +4054,8 @@ var htmx = (function() {
40544054
swapOverride: context.swap,
40554055
select: context.select,
40564056
returnPromise: true,
4057-
pushUrl: context.pushUrl
4057+
push: context.push,
4058+
replace: context.replace
40584059
})
40594060
}
40604061
} else {
@@ -4633,82 +4634,39 @@ var htmx = (function() {
46334634
* @return {HtmxHistoryUpdate}
46344635
*/
46354636
function determineHistoryUpdates(elt, responseInfo) {
4636-
const xhr = responseInfo.xhr
4637+
const { xhr, pathInfo, etc } = responseInfo
46374638

4638-
//= ==========================================
4639-
// First consult response headers
4640-
//= ==========================================
4641-
let pathFromHeaders = null
4642-
let typeFromHeaders = null
4643-
if (hasHeader(xhr, /HX-Push:/i)) {
4644-
pathFromHeaders = xhr.getResponseHeader('HX-Push')
4645-
typeFromHeaders = 'push'
4646-
} else if (hasHeader(xhr, /HX-Push-Url:/i)) {
4647-
pathFromHeaders = xhr.getResponseHeader('HX-Push-Url')
4648-
typeFromHeaders = 'push'
4649-
} else if (hasHeader(xhr, /HX-Replace-Url:/i)) {
4650-
pathFromHeaders = xhr.getResponseHeader('HX-Replace-Url')
4651-
typeFromHeaders = 'replace'
4652-
}
4639+
let push = xhr.getResponseHeader('HX-Push') || xhr.getResponseHeader('HX-Push-Url')
4640+
let replace = xhr.getResponseHeader('HX-Replace-Url')
46534641

46544642
// if there was a response header, that has priority
4655-
if (pathFromHeaders) {
4656-
if (pathFromHeaders === 'false') {
4657-
return {}
4658-
} else {
4659-
return {
4660-
type: typeFromHeaders,
4661-
path: pathFromHeaders
4662-
}
4643+
if (!push && !replace) {
4644+
// Next resolve via DOM values
4645+
push = getClosestAttributeValue(elt, 'hx-push-url') || etc.push
4646+
replace = getClosestAttributeValue(elt, 'hx-replace-url') || etc.replace
4647+
if (!push && !replace && getInternalData(elt).boosted) {
4648+
push = 'true'
46634649
}
46644650
}
46654651

4666-
//= ==========================================
4667-
// Next resolve via DOM values
4668-
//= ==========================================
4669-
const requestPath = responseInfo.pathInfo.finalRequestPath
4670-
const responsePath = responseInfo.pathInfo.responsePath
4671-
4672-
const pushUrl = getClosestAttributeValue(elt, 'hx-push-url') || responseInfo.etc.pushUrl
4673-
const replaceUrl = getClosestAttributeValue(elt, 'hx-replace-url')
4674-
const elementIsBoosted = getInternalData(elt).boosted
4675-
4676-
let saveType = null
4677-
let path = null
4678-
4679-
if (pushUrl) {
4680-
saveType = 'push'
4681-
path = pushUrl
4682-
} else if (replaceUrl) {
4683-
saveType = 'replace'
4684-
path = replaceUrl
4685-
} else if (elementIsBoosted) {
4686-
saveType = 'push'
4687-
path = responsePath || requestPath // if there is no response path, go with the original request path
4652+
let path = push || replace
4653+
// unset or false indicates no push, return empty object
4654+
if (!path || path === 'false') {
4655+
return {}
46884656
}
46894657

4690-
if (path) {
4691-
// false indicates no push, return empty object
4692-
if (path === 'false') {
4693-
return {}
4694-
}
4695-
4696-
// true indicates we want to follow wherever the server ended up sending us
4697-
if (path === 'true') {
4698-
path = responsePath || requestPath // if there is no response path, go with the original request path
4699-
}
4700-
4701-
// restore any anchor associated with the request
4702-
if (responseInfo.pathInfo.anchor && path.indexOf('#') === -1) {
4703-
path = path + '#' + responseInfo.pathInfo.anchor
4704-
}
4658+
// true indicates we want to follow wherever the server ended up sending us
4659+
if (path === 'true') {
4660+
path = pathInfo.responsePath || pathInfo.finalRequestPath // if there is no response path, go with the original request path
4661+
}
4662+
// restore any anchor associated with the request
4663+
if (pathInfo.anchor && path.indexOf('#') === -1) {
4664+
path = path + '#' + pathInfo.anchor
4665+
}
47054666

4706-
return {
4707-
type: saveType,
4708-
path
4709-
}
4710-
} else {
4711-
return {}
4667+
return {
4668+
type: push ? 'push' : 'replace',
4669+
path
47124670
}
47134671
}
47144672

@@ -4798,7 +4756,7 @@ var htmx = (function() {
47984756
redirectPath = redirectSwapSpec.path
47994757
delete redirectSwapSpec.path
48004758
}
4801-
redirectSwapSpec.pushUrl = redirectSwapSpec.pushUrl || 'true'
4759+
redirectSwapSpec.push = redirectSwapSpec.push || 'true'
48024760
ajaxHelper('get', redirectPath, redirectSwapSpec)
48034761
return
48044762
}
@@ -5215,7 +5173,8 @@ var htmx = (function() {
52155173
* @property {Object|FormData} [values]
52165174
* @property {Record<string,string>} [headers]
52175175
* @property {string} [select]
5218-
* @property {string} [pushUrl]
5176+
* @property {string} [push]
5177+
* @property {string} [replace]
52195178
*/
52205179

52215180
/**
@@ -5262,7 +5221,8 @@ var htmx = (function() {
52625221
* @property {Object|FormData} [values]
52635222
* @property {boolean} [credentials]
52645223
* @property {number} [timeout]
5265-
* @property {string} [pushUrl]
5224+
* @property {string} [push]
5225+
* @property {string} [replace]
52665226
*/
52675227

52685228
/**

test/core/api.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,29 +657,59 @@ describe('Core htmx API test', function() {
657657
htmx.process(div.firstChild)
658658
})
659659

660-
it('ajax api pushUrl should push an element into the cache when true', function() {
660+
it('ajax api push Url should push an element into the cache when true', function() {
661661
this.server.respondWith('POST', '/test123', 'Clicked!')
662662

663663
var div = make("<div id='d1'></div>")
664664
htmx.ajax('POST', '/test123', {
665665
target: '#d1',
666666
swap: 'innerHTML',
667-
pushUrl: 'true'
667+
push: 'true'
668668
})
669669
this.server.respond()
670670
div.innerHTML.should.equal('Clicked!')
671671
var path = sessionStorage.getItem('htmx-current-path-for-history')
672672
path.should.equal('/test123')
673673
})
674674

675-
it('ajax api pushUrl should push an element into the cache when string', function() {
675+
it('ajax api push Url should push an element into the cache when string', function() {
676676
this.server.respondWith('POST', '/test', 'Clicked!')
677677

678678
var div = make("<div id='d1'></div>")
679679
htmx.ajax('POST', '/test', {
680680
target: '#d1',
681681
swap: 'innerHTML',
682-
pushUrl: '/abc123'
682+
push: '/abc123'
683+
})
684+
this.server.respond()
685+
div.innerHTML.should.equal('Clicked!')
686+
var path = sessionStorage.getItem('htmx-current-path-for-history')
687+
path.should.equal('/abc123')
688+
})
689+
690+
it('ajax api replace Url should replace an element into the cache when true', function() {
691+
this.server.respondWith('POST', '/test123', 'Clicked!')
692+
693+
var div = make("<div id='d1'></div>")
694+
htmx.ajax('POST', '/test123', {
695+
target: '#d1',
696+
swap: 'innerHTML',
697+
replace: 'true'
698+
})
699+
this.server.respond()
700+
div.innerHTML.should.equal('Clicked!')
701+
var path = sessionStorage.getItem('htmx-current-path-for-history')
702+
path.should.equal('/test123')
703+
})
704+
705+
it('ajax api replace Url should replace an element into the cache when string', function() {
706+
this.server.respondWith('POST', '/test', 'Clicked!')
707+
708+
var div = make("<div id='d1'></div>")
709+
htmx.ajax('POST', '/test', {
710+
target: '#d1',
711+
swap: 'innerHTML',
712+
replace: '/abc123'
683713
})
684714
this.server.respond()
685715
div.innerHTML.should.equal('Clicked!')

test/core/headers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,9 @@ describe('Core htmx AJAX headers', function() {
426426
}, 30)
427427
})
428428

429-
it('should not push new Url on HX-Location if pushUrl false', function(done) {
429+
it('should not push new Url on HX-Location if push Url false', function(done) {
430430
sessionStorage.setItem('htmx-current-path-for-history', '/old')
431-
this.server.respondWith('GET', '/test', [200, { 'HX-Location': '{"pushUrl":"false", "path":"/test2", "target":"#work-area"}' }, ''])
431+
this.server.respondWith('GET', '/test', [200, { 'HX-Location': '{"push":"false", "path":"/test2", "target":"#work-area"}' }, ''])
432432
this.server.respondWith('GET', '/test2', [200, {}, '<div>Yay! Welcome</div>'])
433433
var div = make('<div id="testdiv" hx-trigger="click" hx-get="/test"></div>')
434434
div.click()
@@ -442,11 +442,11 @@ describe('Core htmx AJAX headers', function() {
442442
}, 30)
443443
})
444444

445-
it('should push different Url on HX-Location if pushUrl is string', function(done) {
445+
it('should push different Url on HX-Location if push Url is string', function(done) {
446446
sessionStorage.removeItem('htmx-current-path-for-history')
447447
var HTMX_HISTORY_CACHE_NAME = 'htmx-history-cache'
448448
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
449-
this.server.respondWith('GET', '/test', [200, { 'HX-Location': '{"pushUrl":"/abc123", "path":"/test2", "target":"#work-area"}' }, ''])
449+
this.server.respondWith('GET', '/test', [200, { 'HX-Location': '{"push":"/abc123", "path":"/test2", "target":"#work-area"}' }, ''])
450450
this.server.respondWith('GET', '/test2', [200, {}, '<div>Yay! Welcome</div>'])
451451
var div = make('<div id="testdiv" hx-trigger="click" hx-get="/test"></div>')
452452
div.click()

www/content/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ or
6565
* `values` - values to submit with the request
6666
* `headers` - headers to submit with the request
6767
* `select` - allows you to select the content you want swapped from a response
68-
* `pushUrl` - can be `'true'` or a path to push a URL into browser location history
68+
* `push` - can be `'true'` or a path to push a URL into browser location history
69+
* `replace` - can be `'true'` or a path to replace the URL in the browser location history
6970

7071
##### Example
7172

www/content/headers/hx-location.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Path is required and is url to load the response from. The rest of the data mirr
3030
* `values` - values to submit with the request
3131
* `headers` - headers to submit with the request
3232
* `select` - allows you to select the content you want swapped from a response
33-
* `pushUrl` - set to `'false'` or a path string to prevent or override the URL pushed to browser location history
33+
* `push` - set to `'false'` or a path string to prevent or override the URL pushed to browser location history
34+
* `replace` - a path string to prevent or override the URL replaced in the browser location history
3435

3536
## Notes
3637

0 commit comments

Comments
 (0)