mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-04 00:05:20 +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
|
## TODOS
|
||||||
|
|
||||||
* history support
|
* settle formalization
|
||||||
* Implement LRU
|
* focus recapture
|
||||||
* Issue GET to restore content if there isn't a copy locally
|
* polling cancellation API 205 code
|
||||||
* polling cancellation API
|
* meta config tag
|
||||||
|
* simple logging API
|
||||||
|
|
||||||
* Testing
|
* Testing
|
||||||
* polling
|
* polling
|
||||||
* merge
|
* merge
|
||||||
@ -23,7 +25,6 @@
|
|||||||
* history
|
* history
|
||||||
* hx-boost
|
* hx-boost
|
||||||
* transition model for content swaps
|
* transition model for content swaps
|
||||||
* distribute on https://unpkg.com/
|
|
||||||
* build website with 11ty
|
* build website with 11ty
|
||||||
* landing page
|
* landing page
|
||||||
* docs page
|
* docs page
|
||||||
|
27
src/htmx.js
27
src/htmx.js
@ -584,8 +584,8 @@ var HTMx = HTMx || (function () {
|
|||||||
// History Support
|
// History Support
|
||||||
//====================================================================
|
//====================================================================
|
||||||
function getHistoryElement() {
|
function getHistoryElement() {
|
||||||
var historyElt = getDocument().getElementsByClassName('hx-history-element');
|
var historyElt = getDocument().querySelector('.hx-history-element');
|
||||||
return historyElt.length > 0 ? historyElt[0] : getDocument().body;
|
return historyElt || getDocument().body;
|
||||||
}
|
}
|
||||||
|
|
||||||
function purgeOldestPaths(paths, historyTimestamps) {
|
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() {
|
function restoreHistory() {
|
||||||
var pathAndSearch = location.pathname+location.search;
|
var pathAndSearch = location.pathname+location.search;
|
||||||
triggerEvent(getDocument().body, "historyUpdate.hx", {path:pathAndSearch});
|
triggerEvent(getDocument().body, "historyUpdate.hx", {path:pathAndSearch});
|
||||||
bumpHistoryAccessDate(pathAndSearch);
|
|
||||||
var content = localStorage.getItem('hx-history-content-' + pathAndSearch);
|
var content = localStorage.getItem('hx-history-content-' + pathAndSearch);
|
||||||
var elt = getHistoryElement();
|
if (content) {
|
||||||
swapInnerHTML(elt, makeFragment(content));
|
bumpHistoryAccessDate(pathAndSearch);
|
||||||
|
swapInnerHTML(getHistoryElement(), makeFragment(content));
|
||||||
|
} else {
|
||||||
|
loadHistoryFromServer(pathAndSearch);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldPush(elt) {
|
function shouldPush(elt) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user