Skip to content

Commit 83ec92f

Browse files
committed
Fix: cache handling
1 parent e08adc3 commit 83ec92f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Resources/Private/Main.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import lscache from "lscache";
22

3-
lscache.setBucket("icons");
3+
const cacheName = "icon:";
4+
5+
function setCache(name, value) {
6+
lscache.setBucket(cacheName);
7+
// Store for 1 week 60 min * 24 hours * 7 days = 10080
8+
lscache.set(name, value, 10080);
9+
lscache.resetBucket();
10+
}
11+
12+
function getCache(name) {
13+
lscache.setBucket(cacheName);
14+
const value = lscache.get(name);
15+
lscache.resetBucket();
16+
return value;
17+
}
418

519
function replaceTag(element, markup) {
620
const attributes = [...element.attributes]
@@ -18,7 +32,7 @@ function replaceTag(element, markup) {
1832
window.addEventListener("alpine:init", () => {
1933
window.Alpine.data("icon", (segment) => ({
2034
init() {
21-
const cache = lscache.get(segment);
35+
const cache = getCache(segment);
2236
if (cache && typeof cache === "string") {
2337
replaceTag(this.$el, cache);
2438
return;
@@ -31,8 +45,7 @@ window.addEventListener("alpine:init", () => {
3145
return response.text();
3246
})
3347
.then((data) => {
34-
// Store for 1 week 60 min * 24 hours * 7 days = 10080
35-
lscache.set(segment, data, 10080);
48+
setCache(segment, data);
3649
replaceTag(this.$el, data);
3750
})
3851
.catch((error) => {

0 commit comments

Comments
 (0)