load content from server on history cache miss

This commit is contained in:
carson 2020-05-05 02:59:48 -07:00
parent 8a4449faae
commit eb9ea0cbca
2 changed files with 28 additions and 10 deletions

11
TODO.md
View File

@ -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

View File

@ -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) {