From d018ef4f0f76ddba86fb2e5656002669422febde Mon Sep 17 00:00:00 2001 From: carson Date: Tue, 13 Oct 2020 17:02:15 -0600 Subject: [PATCH] catch on history cache save fixes https://github.com/bigskysoftware/htmx/issues/209 --- src/htmx.js | 6 +++++- test/attributes/hx-push-url.js | 16 ++++++++++++++++ www/events.md | 8 ++++++++ www/reference.md | 1 + 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/htmx.js b/src/htmx.js index 563f057a..87d76f2b 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -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) { diff --git a/test/attributes/hx-push-url.js b/test/attributes/hx-push-url.js index 3dd6fba0..604a6d3c 100644 --- a/test/attributes/hx-push-url.js +++ b/test/attributes/hx-push-url.js @@ -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"); + } + }); + }); diff --git a/www/events.md b/www/events.md index 1f5a4c34..f6baf199 100644 --- a/www/events.md +++ b/www/events.md @@ -111,6 +111,14 @@ than a single value. * `detail.target` - the target of the request * `detail.verb` - the HTTP verb in use +### 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` + ### Event - [`htmx:historyCacheMiss`](#htmx:historyCacheMiss) This event is triggered when a cache miss occurs when restoring history diff --git a/www/reference.md b/www/reference.md index b6186a10..ce4709bf 100644 --- a/www/reference.md +++ b/www/reference.md @@ -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