Skip to content

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/utils/localStorage.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* A utility class for safely interacting with localStorage.
33
*/
44
export class LocalStorage {
5-
static store: Storage | undefined = globalThis.localStorage;
5+
static store: Storage | undefined = ensureLocalStorage();
66

77
/**
88
* Safely get a value from localStorage.
@@ -51,3 +51,14 @@ export class LocalStorage {
5151
this.store?.removeItem(key);
5252
}
5353
}
54+
55+
function ensureLocalStorage(): Storage | undefined {
56+
try {
57+
const testKey = "__test_localStorage__";
58+
globalThis.localStorage.setItem(testKey, testKey);
59+
globalThis.localStorage.removeItem(testKey);
60+
return globalThis.localStorage;
61+
} catch {
62+
return undefined;
63+
}
64+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"bundlesize": [
9696
{
9797
"path": "./dist/search-insights.min.js",
98-
"maxSize": "3.60 kB"
98+
"maxSize": "3.70 kB"
9999
}
100100
],
101101
"packageManager": "[email protected]"

0 commit comments

Comments
 (0)