Skip to content

Commit

Permalink
🔒️ Handle when local storage isn’t available (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Dec 5, 2024
1 parent d3c721a commit 386231d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions apps/web/src/utils/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ class LocalStorage {
private isLocalStorageAvailable: boolean;

constructor() {
this.isLocalStorageAvailable =
typeof window !== "undefined" && !!window.localStorage;
this.isLocalStorageAvailable = this.checkLocalStorageAvailability();
}

private checkLocalStorageAvailability(): boolean {
try {
if (typeof window === "undefined") return false;

// Test localStorage by actually trying to use it
const testKey = "__storage_test__";
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
} catch (e) {
return false;
}
}

getItem(key: string): string | null {
Expand Down

0 comments on commit 386231d

Please sign in to comment.