Skip to content

Commit 8ce596e

Browse files
authored
Clearing expired local storage items on initialization
2 parents 333b5a4 + edd032c commit 8ce596e

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src/gist.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import EventEmitter from "./utilities/event-emitter";
22
import { log } from "./utilities/log";
3+
import { clearExpiredFromLocalStore } from "./utilities/local-storage";
34
import { startQueueListener, checkMessageQueue, stopSSEListener } from "./managers/queue-manager";
45
import { setUserToken, clearUserToken, useGuestSession } from "./managers/user-manager";
56
import { showMessage, embedMessage, hideMessage, removePersistentMessage, fetchMessageByInstanceId, logBroadcastDismissedLocally } from "./managers/message-manager";
@@ -23,6 +24,7 @@ export default class {
2324
this.currentRoute = null;
2425
this.isDocumentVisible = true;
2526
this.config.isPreviewSession = setupPreview();
27+
clearExpiredFromLocalStore();
2628

2729
log(`Setup complete on ${this.config.env} environment.`);
2830

src/utilities/local-storage.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { log } from "./log";
2+
13
const maxExpiryDays = 365;
24

35
const isPersistingSessionLocalStoreName = "gist.web.isPersistingSession";
@@ -21,32 +23,20 @@ export function setKeyToLocalStore(key, value, ttl = null) {
2123
}
2224

2325
export function getKeyFromLocalStore(key) {
24-
const itemStr = getStorage().getItem(key);
25-
if (!itemStr) return null;
26-
27-
const item = JSON.parse(itemStr);
28-
const now = new Date();
29-
const expiryTime = new Date(item.expiry);
30-
31-
// Retroactive bugfix: remove old cache entries with long expiry times
32-
const isBroadcastOrUserKey = (key.startsWith("gist.web.message.broadcasts") && !key.endsWith("shouldShow") && !key.endsWith("numberOfTimesShown")) || (key.startsWith("gist.web.message.user") && !key.endsWith("seen"));
33-
const sixtyMinutesFromNow = new Date(now.getTime() + 61 * 60 * 1000);
34-
if (isBroadcastOrUserKey && expiryTime.getTime() > sixtyMinutesFromNow.getTime()) {
35-
clearKeyFromLocalStore(key);
36-
return null;
37-
}
38-
39-
if (now.getTime() > expiryTime.getTime()) {
40-
clearKeyFromLocalStore(key);
41-
return null;
42-
}
43-
return item.value;
26+
return checkKeyForExpiry(key);
4427
}
4528

4629
export function clearKeyFromLocalStore(key) {
4730
getStorage().removeItem(key);
4831
}
4932

33+
export function clearExpiredFromLocalStore() {
34+
const storage = getStorage();
35+
for (let i = storage.length - 1; i >= 0; i--) {
36+
checkKeyForExpiry(storage.key(i));
37+
}
38+
}
39+
5040
export function isSessionBeingPersisted() {
5141
const currentValue = sessionStorage.getItem(isPersistingSessionLocalStoreName);
5242
if (currentValue === null) {
@@ -59,4 +49,38 @@ export function isSessionBeingPersisted() {
5949
// Helper function to select the correct storage based on the session flag
6050
function getStorage() {
6151
return isSessionBeingPersisted() ? localStorage : sessionStorage;
52+
}
53+
54+
function checkKeyForExpiry(key) {
55+
if (!key) return null;
56+
57+
try {
58+
const itemStr = getStorage().getItem(key);
59+
if (!itemStr) return null;
60+
61+
const item = JSON.parse(itemStr);
62+
if (!item.expiry) return item.value;
63+
64+
const now = new Date();
65+
const expiryTime = new Date(item.expiry);
66+
67+
// remove old cache entries with long expiry times
68+
const isBroadcastOrUserKey = (key.startsWith("gist.web.message.broadcasts") && !key.endsWith("shouldShow") && !key.endsWith("numberOfTimesShown")) || (key.startsWith("gist.web.message.user") && !key.endsWith("seen"));
69+
const sixtyMinutesFromNow = new Date(now.getTime() + 61 * 60 * 1000);
70+
if (isBroadcastOrUserKey && expiryTime.getTime() > sixtyMinutesFromNow.getTime()) {
71+
clearKeyFromLocalStore(key);
72+
return null;
73+
}
74+
75+
if (now.getTime() > expiryTime.getTime()) {
76+
clearKeyFromLocalStore(key);
77+
return null;
78+
}
79+
80+
return item.value;
81+
} catch (e) {
82+
log(`Error checking key ${key} for expiry: ${e}`);
83+
}
84+
85+
return null;
6286
}

0 commit comments

Comments
 (0)