catch on history cache save

fixes https://github.com/bigskysoftware/htmx/issues/209
This commit is contained in:
carson 2020-10-13 17:02:15 -06:00
parent e80169808c
commit d018ef4f0f
4 changed files with 30 additions and 1 deletions

View File

@ -1179,7 +1179,11 @@ return (function () {
while (historyCache.length > htmx.config.historyCacheSize) {
historyCache.shift();
}
localStorage.setItem("htmx-history-cache", JSON.stringify(historyCache));
try {
localStorage.setItem("htmx-history-cache", JSON.stringify(historyCache));
} catch (e) {
triggerErrorEvent(getDocument().body, "htmx:historyCacheError", {cause:e})
}
}
function getCachedHistory(url) {

View File

@ -188,4 +188,20 @@ describe("hx-push-url attribute", function() {
}
});
it("saveToHistoryCache should not throw", function () {
var bigContent = "Dummy";
for (var i = 0; i < 20; i++) {
bigContent += bigContent;
}
console.log(bigContent.length);
try {
localStorage.removeItem("htmx-history-cache");
htmx._("saveToHistoryCache")("/dummy", bigContent, "Foo", 0);
should.equal(localStorage.getItem("htmx-history-cache"), null);
} finally {
// clear history cache afterwards
localStorage.removeItem("htmx-history-cache");
}
});
});

View File

@ -111,6 +111,14 @@ than a single value.
* `detail.target` - the target of the request
* `detail.verb` - the HTTP verb in use
### <a name="htmx:historyCacheError"></a> Event - [`htmx:historyCacheError`](#htmx:historyCacheError)
This event is triggered when an attempt to save the cache to `localStorage` fails
##### Details
* `detail.cause` - the `Exception` that was thrown when attempting to save history to `localStorage`
### <a name="htmx:historyCacheMiss"></a> Event - [`htmx:historyCacheMiss`](#htmx:historyCacheMiss)
This event is triggered when a cache miss occurs when restoring history

View File

@ -104,6 +104,7 @@ title: </> htmx - Attributes
| [`htmx:beforeRequest`](/events#htmx:beforeRequest) | triggered before an AJAX request is made
| [`htmx:beforeSwap`](/events#htmx:beforeSwap) | triggered before a swap is done
| [`htmx:configRequest`](/events#htmx:configRequest) | triggered before the request, allows you to customize parameters, headers
| [`htmx:historyCacheError`](/events#htmx:historyCacheError) | triggered on an error during cache writing
| [`htmx:historyCacheMiss`](/events#htmx:historyCacheMiss) | triggered on a cache miss in the history subsystem
| [`htmx:historyCacheMissError`](/events#htmx:historyCacheMissError) | triggered on a unsuccessful remote retrieval
| [`htmx:historyCacheMissLoad`](/events#htmx:historyCacheMissLoad) | triggered on a succesful remote retrieval