discard least recently used items if cache saving fails until empty

fixes https://github.com/bigskysoftware/htmx/issues/377
This commit is contained in:
carson 2021-02-19 09:41:40 -07:00
parent 11287d924b
commit 36e6020942

View File

@ -1418,10 +1418,14 @@ return (function () {
while (historyCache.length > htmx.config.historyCacheSize) {
historyCache.shift();
}
try {
localStorage.setItem("htmx-history-cache", JSON.stringify(historyCache));
} catch (e) {
triggerErrorEvent(getDocument().body, "htmx:historyCacheError", {cause:e})
while(historyCache.length > 0){
try {
localStorage.setItem("htmx-history-cache", JSON.stringify(historyCache));
return;
} catch (e) {
triggerErrorEvent(getDocument().body, "htmx:historyCacheError", {cause:e, cache: historyCache})
historyCache.shift(); // shrink the cache and retry
}
}
}