mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-09-28 21:41:40 +00:00
load content from server on history cache miss
This commit is contained in:
parent
8a4449faae
commit
eb9ea0cbca
11
TODO.md
11
TODO.md
@ -12,10 +12,12 @@
|
||||
|
||||
## TODOS
|
||||
|
||||
* history support
|
||||
* Implement LRU
|
||||
* Issue GET to restore content if there isn't a copy locally
|
||||
* polling cancellation API
|
||||
* settle formalization
|
||||
* focus recapture
|
||||
* polling cancellation API 205 code
|
||||
* meta config tag
|
||||
* simple logging API
|
||||
|
||||
* Testing
|
||||
* polling
|
||||
* merge
|
||||
@ -23,7 +25,6 @@
|
||||
* history
|
||||
* hx-boost
|
||||
* transition model for content swaps
|
||||
* distribute on https://unpkg.com/
|
||||
* build website with 11ty
|
||||
* landing page
|
||||
* docs page
|
||||
|
27
src/htmx.js
27
src/htmx.js
@ -584,8 +584,8 @@ var HTMx = HTMx || (function () {
|
||||
// History Support
|
||||
//====================================================================
|
||||
function getHistoryElement() {
|
||||
var historyElt = getDocument().getElementsByClassName('hx-history-element');
|
||||
return historyElt.length > 0 ? historyElt[0] : getDocument().body;
|
||||
var historyElt = getDocument().querySelector('.hx-history-element');
|
||||
return historyElt || getDocument().body;
|
||||
}
|
||||
|
||||
function purgeOldestPaths(paths, historyTimestamps) {
|
||||
@ -628,13 +628,30 @@ var HTMx = HTMx || (function () {
|
||||
}
|
||||
}
|
||||
|
||||
function loadHistoryFromServer(pathAndSearch) {
|
||||
triggerEvent(getDocument().body, "historyCacheMiss.hx", {path: pathAndSearch});
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', pathAndSearch, true);
|
||||
request.onload = function () {
|
||||
triggerEvent(getDocument().body, "historyCacheMissLoad.hx", {path: pathAndSearch});
|
||||
if (this.status >= 200 && this.status < 400) {
|
||||
var fragment = makeFragment(this.response);
|
||||
fragment = fragment.querySelector('.hx-history-element') || fragment;
|
||||
swapInnerHTML(getHistoryElement(), fragment);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function restoreHistory() {
|
||||
var pathAndSearch = location.pathname+location.search;
|
||||
triggerEvent(getDocument().body, "historyUpdate.hx", {path:pathAndSearch});
|
||||
bumpHistoryAccessDate(pathAndSearch);
|
||||
var content = localStorage.getItem('hx-history-content-' + pathAndSearch);
|
||||
var elt = getHistoryElement();
|
||||
swapInnerHTML(elt, makeFragment(content));
|
||||
if (content) {
|
||||
bumpHistoryAccessDate(pathAndSearch);
|
||||
swapInnerHTML(getHistoryElement(), makeFragment(content));
|
||||
} else {
|
||||
loadHistoryFromServer(pathAndSearch);
|
||||
}
|
||||
}
|
||||
|
||||
function shouldPush(elt) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user