File tree Expand file tree Collapse file tree 1 file changed +27
-3
lines changed
Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments