mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-09-26 20:40:41 +00:00
Move currentPathForHistory to session storage (#3330)
* Move currentPathForHistory to session storage * update test * fix more sessionStorage from localStorage
This commit is contained in:
parent
c9e2bea954
commit
c091b95fa3
24
src/htmx.js
24
src/htmx.js
@ -3115,6 +3115,16 @@ var htmx = (function() {
|
||||
//= ===================================================================
|
||||
let currentPathForHistory = location.pathname + location.search
|
||||
|
||||
/**
|
||||
* @param {string} path
|
||||
*/
|
||||
function setCurrentPathForHistory(path) {
|
||||
currentPathForHistory = path
|
||||
if (canAccessLocalStorage()) {
|
||||
sessionStorage.setItem('htmx-current-path-for-history', path)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Element}
|
||||
*/
|
||||
@ -3222,7 +3232,11 @@ var htmx = (function() {
|
||||
|
||||
function saveCurrentPageToHistory() {
|
||||
const elt = getHistoryElement()
|
||||
const path = currentPathForHistory || location.pathname + location.search
|
||||
let path = currentPathForHistory
|
||||
if (canAccessLocalStorage()) {
|
||||
path = sessionStorage.getItem('htmx-current-path-for-history')
|
||||
}
|
||||
path = path || location.pathname + location.search
|
||||
|
||||
// Allow history snapshot feature to be disabled where hx-history="false"
|
||||
// is present *anywhere* in the current document we're about to save,
|
||||
@ -3252,7 +3266,7 @@ var htmx = (function() {
|
||||
if (htmx.config.historyEnabled) {
|
||||
history.pushState({ htmx: true }, '', path)
|
||||
}
|
||||
currentPathForHistory = path
|
||||
setCurrentPathForHistory(path)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3260,7 +3274,7 @@ var htmx = (function() {
|
||||
*/
|
||||
function replaceUrlInHistory(path) {
|
||||
if (htmx.config.historyEnabled) history.replaceState({ htmx: true }, '', path)
|
||||
currentPathForHistory = path
|
||||
setCurrentPathForHistory(path)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3293,7 +3307,7 @@ var htmx = (function() {
|
||||
contextElement: details.historyElt,
|
||||
historyRequest: true
|
||||
})
|
||||
currentPathForHistory = details.path
|
||||
setCurrentPathForHistory(details.path)
|
||||
triggerEvent(getDocument().body, 'htmx:historyRestore', { path, cacheMiss: true, serverResponse: details.response })
|
||||
} else {
|
||||
triggerErrorEvent(getDocument().body, 'htmx:historyCacheMissLoadError', details)
|
||||
@ -3319,7 +3333,7 @@ var htmx = (function() {
|
||||
contextElement: details.historyElt,
|
||||
title: cached.title
|
||||
})
|
||||
currentPathForHistory = details.path
|
||||
setCurrentPathForHistory(details.path)
|
||||
triggerEvent(getDocument().body, 'htmx:historyRestore', details)
|
||||
}
|
||||
} else {
|
||||
|
@ -4,12 +4,12 @@ describe('hx-history attribute', function() {
|
||||
beforeEach(function() {
|
||||
this.server = makeServer()
|
||||
clearWorkArea()
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
})
|
||||
afterEach(function() {
|
||||
this.server.restore()
|
||||
clearWorkArea()
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
})
|
||||
|
||||
it('content of hx-history-elt is used during history replacment', function() {
|
||||
@ -29,7 +29,7 @@ describe('hx-history attribute', function() {
|
||||
|
||||
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>')
|
||||
// clear cache so it makes a full page request on history restore
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
|
||||
htmx._('restoreHistory')('/test1')
|
||||
this.server.respond()
|
||||
|
@ -306,7 +306,7 @@ describe('hx-push-url attribute', function() {
|
||||
} finally {
|
||||
htmx.config.getCacheBusterParam = false
|
||||
}
|
||||
htmx._('currentPathForHistory').should.equal('/test')
|
||||
sessionStorage.getItem('htmx-current-path-for-history').should.equal('/test')
|
||||
})
|
||||
|
||||
it('ensure history pushState called', function() {
|
||||
|
@ -741,7 +741,7 @@ describe('Core htmx Events', function() {
|
||||
})
|
||||
|
||||
it('preventDefault() in htmx:historyCacheMiss stops the history request', function() {
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
var handler = htmx.on('htmx:historyCacheMiss', function(evt) {
|
||||
evt.preventDefault()
|
||||
})
|
||||
@ -760,7 +760,7 @@ describe('Core htmx Events', function() {
|
||||
this.server.respond()
|
||||
workArea.textContent.should.equal('test2')
|
||||
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
|
||||
htmx._('restoreHistory')('/test1')
|
||||
this.server.respond()
|
||||
getWorkArea().textContent.should.equal('test2')
|
||||
@ -770,7 +770,7 @@ describe('Core htmx Events', function() {
|
||||
})
|
||||
|
||||
it('htmx:historyCacheMissLoad event can update history swap', function() {
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
var handler = htmx.on('htmx:historyCacheMissLoad', function(evt) {
|
||||
evt.detail.historyElt = byId('hist-re-target')
|
||||
evt.detail.swapSpec.swapStyle = 'outerHTML'
|
||||
@ -793,7 +793,7 @@ describe('Core htmx Events', function() {
|
||||
this.server.respond()
|
||||
workArea.textContent.should.equal('test2')
|
||||
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
|
||||
htmx._('restoreHistory')('/test1')
|
||||
this.server.respond()
|
||||
getWorkArea().textContent.should.equal('test2Updated')
|
||||
@ -805,7 +805,7 @@ describe('Core htmx Events', function() {
|
||||
})
|
||||
|
||||
it('htmx:historyCacheMiss event can set custom request headers', function() {
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
var handler = htmx.on('htmx:historyCacheMiss', function(evt) {
|
||||
evt.detail.xhr.setRequestHeader('CustomHeader', 'true')
|
||||
})
|
||||
@ -816,7 +816,7 @@ describe('Core htmx Events', function() {
|
||||
make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>')
|
||||
|
||||
try {
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
|
||||
htmx._('restoreHistory')('/test1')
|
||||
this.server.respond()
|
||||
getWorkArea().textContent.should.equal('test1')
|
||||
@ -826,7 +826,7 @@ describe('Core htmx Events', function() {
|
||||
})
|
||||
|
||||
it('preventDefault() in htmx:historyCacheHit stops the history action', function() {
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
var handler = htmx.on('htmx:historyCacheHit', function(evt) {
|
||||
evt.preventDefault()
|
||||
})
|
||||
@ -853,7 +853,7 @@ describe('Core htmx Events', function() {
|
||||
})
|
||||
|
||||
it('htmx:historyCacheHit event can update history swap', function() {
|
||||
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
|
||||
var handler = htmx.on('htmx:historyCacheHit', function(evt) {
|
||||
evt.detail.historyElt = byId('hist-re-target')
|
||||
evt.detail.swapSpec.swapStyle = 'outerHTML'
|
||||
|
Loading…
x
Reference in New Issue
Block a user