Skip to content

Commit 079cdba

Browse files
committed
Remove expired sections in localStorage
1 parent 683c614 commit 079cdba

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

view/frontend/templates/script/store/localstorage-store.phtml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,24 @@ use Magento\Framework\View\Element\Template;
1515
data: {},
1616
init() {
1717
const storedData = localStorage.getItem(this.key);
18-
this.data = storedData ? JSON.parse(storedData) : null;
18+
this.data = storedData ? JSON.parse(storedData) : {};
1919

2020
if (!this.data) {
2121
this.refresh();
2222
}
23+
24+
let changed = false;
25+
Object.entries(this.data).forEach(([key, value]) => {
26+
if (value.data_id && value.data_id < Date. now()) {
27+
delete this.data[key];
28+
changed = true;
29+
}
30+
})
31+
32+
33+
if (changed) {
34+
localStorage.setItem(this.key, JSON.stringify(this.data));
35+
}
2336
},
2437
save() {
2538
localStorage.setItem(this.key, JSON.stringify(this.data));

0 commit comments

Comments
 (0)