Skip to content

Commit 177d877

Browse files
authored
Fix for earlier implemented 404 page not rendering due to mismatch in theme structure in localstorage and cookies (#1055)
1 parent fe4085e commit 177d877

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/utils/storage.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,44 @@ export class StorageManager<T> {
1010
set(value: T): void {
1111
const valueToSet = JSON.stringify(value)
1212
Cookies.set(this.key, valueToSet, { expires: 365 })
13+
// sync with localStorage
14+
localStorage.setItem('theme', value as string)
1315
}
1416

1517
get(): T | undefined {
16-
const value = Cookies.get(this.key)
18+
// 1. Try cookie first
19+
const cookieValue = Cookies.get(this.key)
20+
if (cookieValue) {
21+
try {
22+
console.log("cookie", Cookies.get())
23+
console.log("cookieValue", cookieValue)
24+
console.log("lsValue", localStorage)
25+
return JSON.parse(cookieValue)
26+
} catch {
27+
return cookieValue as unknown as T
28+
}
29+
}
1730

18-
if (value) {
19-
return JSON.parse(value)
31+
// 2. Fallback to localStorage
32+
const lsValue = localStorage.getItem('theme')
33+
if (lsValue) {
34+
try {
35+
const parsed = JSON.parse(lsValue)
36+
this.set(parsed as T)
37+
console.log("lsValue", lsValue)
38+
return parsed
39+
} catch {
40+
this.set(lsValue as unknown as T)
41+
return lsValue as unknown as T
42+
}
2043
}
2144

2245
return undefined
2346
}
2447

2548
remove(): void {
2649
Cookies.remove(this.key)
50+
localStorage.removeItem('theme')
2751
}
2852
}
2953

0 commit comments

Comments
 (0)