Skip to content

Commit c091b95

Browse files
Move currentPathForHistory to session storage (#3330)
* Move currentPathForHistory to session storage * update test * fix more sessionStorage from localStorage
1 parent c9e2bea commit c091b95

4 files changed

Lines changed: 31 additions & 17 deletions

File tree

src/htmx.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,6 +3115,16 @@ var htmx = (function() {
31153115
//= ===================================================================
31163116
let currentPathForHistory = location.pathname + location.search
31173117

3118+
/**
3119+
* @param {string} path
3120+
*/
3121+
function setCurrentPathForHistory(path) {
3122+
currentPathForHistory = path
3123+
if (canAccessLocalStorage()) {
3124+
sessionStorage.setItem('htmx-current-path-for-history', path)
3125+
}
3126+
}
3127+
31183128
/**
31193129
* @returns {Element}
31203130
*/
@@ -3222,7 +3232,11 @@ var htmx = (function() {
32223232

32233233
function saveCurrentPageToHistory() {
32243234
const elt = getHistoryElement()
3225-
const path = currentPathForHistory || location.pathname + location.search
3235+
let path = currentPathForHistory
3236+
if (canAccessLocalStorage()) {
3237+
path = sessionStorage.getItem('htmx-current-path-for-history')
3238+
}
3239+
path = path || location.pathname + location.search
32263240

32273241
// Allow history snapshot feature to be disabled where hx-history="false"
32283242
// is present *anywhere* in the current document we're about to save,
@@ -3252,15 +3266,15 @@ var htmx = (function() {
32523266
if (htmx.config.historyEnabled) {
32533267
history.pushState({ htmx: true }, '', path)
32543268
}
3255-
currentPathForHistory = path
3269+
setCurrentPathForHistory(path)
32563270
}
32573271

32583272
/**
32593273
* @param {string} path
32603274
*/
32613275
function replaceUrlInHistory(path) {
32623276
if (htmx.config.historyEnabled) history.replaceState({ htmx: true }, '', path)
3263-
currentPathForHistory = path
3277+
setCurrentPathForHistory(path)
32643278
}
32653279

32663280
/**
@@ -3293,7 +3307,7 @@ var htmx = (function() {
32933307
contextElement: details.historyElt,
32943308
historyRequest: true
32953309
})
3296-
currentPathForHistory = details.path
3310+
setCurrentPathForHistory(details.path)
32973311
triggerEvent(getDocument().body, 'htmx:historyRestore', { path, cacheMiss: true, serverResponse: details.response })
32983312
} else {
32993313
triggerErrorEvent(getDocument().body, 'htmx:historyCacheMissLoadError', details)
@@ -3319,7 +3333,7 @@ var htmx = (function() {
33193333
contextElement: details.historyElt,
33203334
title: cached.title
33213335
})
3322-
currentPathForHistory = details.path
3336+
setCurrentPathForHistory(details.path)
33233337
triggerEvent(getDocument().body, 'htmx:historyRestore', details)
33243338
}
33253339
} else {

test/attributes/hx-history-elt.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ describe('hx-history attribute', function() {
44
beforeEach(function() {
55
this.server = makeServer()
66
clearWorkArea()
7-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
7+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
88
})
99
afterEach(function() {
1010
this.server.restore()
1111
clearWorkArea()
12-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
12+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
1313
})
1414

1515
it('content of hx-history-elt is used during history replacment', function() {
@@ -29,7 +29,7 @@ describe('hx-history attribute', function() {
2929

3030
this.server.respondWith('GET', '/test1', '<div>content outside of hx-history-elt not included</div><div id="work-area" hx-history-elt><div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test3</div></div>')
3131
// clear cache so it makes a full page request on history restore
32-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
32+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
3333

3434
htmx._('restoreHistory')('/test1')
3535
this.server.respond()

test/attributes/hx-push-url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe('hx-push-url attribute', function() {
306306
} finally {
307307
htmx.config.getCacheBusterParam = false
308308
}
309-
htmx._('currentPathForHistory').should.equal('/test')
309+
sessionStorage.getItem('htmx-current-path-for-history').should.equal('/test')
310310
})
311311

312312
it('ensure history pushState called', function() {

test/core/events.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ describe('Core htmx Events', function() {
741741
})
742742

743743
it('preventDefault() in htmx:historyCacheMiss stops the history request', function() {
744-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
744+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
745745
var handler = htmx.on('htmx:historyCacheMiss', function(evt) {
746746
evt.preventDefault()
747747
})
@@ -760,7 +760,7 @@ describe('Core htmx Events', function() {
760760
this.server.respond()
761761
workArea.textContent.should.equal('test2')
762762

763-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
763+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
764764
htmx._('restoreHistory')('/test1')
765765
this.server.respond()
766766
getWorkArea().textContent.should.equal('test2')
@@ -770,7 +770,7 @@ describe('Core htmx Events', function() {
770770
})
771771

772772
it('htmx:historyCacheMissLoad event can update history swap', function() {
773-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
773+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
774774
var handler = htmx.on('htmx:historyCacheMissLoad', function(evt) {
775775
evt.detail.historyElt = byId('hist-re-target')
776776
evt.detail.swapSpec.swapStyle = 'outerHTML'
@@ -793,7 +793,7 @@ describe('Core htmx Events', function() {
793793
this.server.respond()
794794
workArea.textContent.should.equal('test2')
795795

796-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
796+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
797797
htmx._('restoreHistory')('/test1')
798798
this.server.respond()
799799
getWorkArea().textContent.should.equal('test2Updated')
@@ -805,7 +805,7 @@ describe('Core htmx Events', function() {
805805
})
806806

807807
it('htmx:historyCacheMiss event can set custom request headers', function() {
808-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
808+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
809809
var handler = htmx.on('htmx:historyCacheMiss', function(evt) {
810810
evt.detail.xhr.setRequestHeader('CustomHeader', 'true')
811811
})
@@ -816,7 +816,7 @@ describe('Core htmx Events', function() {
816816
make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>')
817817

818818
try {
819-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
819+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
820820
htmx._('restoreHistory')('/test1')
821821
this.server.respond()
822822
getWorkArea().textContent.should.equal('test1')
@@ -826,7 +826,7 @@ describe('Core htmx Events', function() {
826826
})
827827

828828
it('preventDefault() in htmx:historyCacheHit stops the history action', function() {
829-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
829+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
830830
var handler = htmx.on('htmx:historyCacheHit', function(evt) {
831831
evt.preventDefault()
832832
})
@@ -853,7 +853,7 @@ describe('Core htmx Events', function() {
853853
})
854854

855855
it('htmx:historyCacheHit event can update history swap', function() {
856-
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
856+
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
857857
var handler = htmx.on('htmx:historyCacheHit', function(evt) {
858858
evt.detail.historyElt = byId('hist-re-target')
859859
evt.detail.swapSpec.swapStyle = 'outerHTML'

0 commit comments

Comments
 (0)