-
Notifications
You must be signed in to change notification settings - Fork 60.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: safaLocalStorage #5386
fix: safaLocalStorage #5386
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,13 @@ import ar from "./ar"; | |
import bn from "./bn"; | ||
import sk from "./sk"; | ||
import { merge } from "../utils/merge"; | ||
import { safeLocalStorage } from "@/app/utils"; | ||
|
||
import type { LocaleType } from "./cn"; | ||
export type { LocaleType, PartialLocaleType } from "./cn"; | ||
|
||
const localStorage = safeLocalStorage(); | ||
|
||
const ALL_LANGS = { | ||
cn, | ||
en, | ||
|
@@ -82,17 +85,11 @@ merge(fallbackLang, targetLang); | |
export default fallbackLang as LocaleType; | ||
|
||
function getItem(key: string) { | ||
try { | ||
return localStorage.getItem(key); | ||
} catch { | ||
return null; | ||
} | ||
return localStorage.getItem(key); | ||
} | ||
|
||
function setItem(key: string, value: string) { | ||
try { | ||
localStorage.setItem(key, value); | ||
} catch {} | ||
localStorage.setItem(key, value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Approved simplification of The Consider adding a comment like this: # Directly using `localStorage.setItem` as `safeLocalStorage` handles errors internally |
||
} | ||
|
||
function getLanguage() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,3 +318,63 @@ export function adapter(config: Record<string, unknown>) { | |
: path; | ||
return fetch(fetchUrl as string, { ...rest, responseType: "text" }); | ||
} | ||
|
||
export function safeLocalStorage(): { | ||
getItem: (key: string) => string | null; | ||
setItem: (key: string, value: string) => void; | ||
removeItem: (key: string) => void; | ||
clear: () => void; | ||
} { | ||
let storage: Storage | null; | ||
|
||
try { | ||
if (typeof window !== "undefined" && window.localStorage) { | ||
storage = window.localStorage; | ||
} else { | ||
storage = null; | ||
} | ||
} catch (e) { | ||
console.error("localStorage is not available:", e); | ||
storage = null; | ||
} | ||
|
||
return { | ||
getItem(key: string): string | null { | ||
if (storage) { | ||
return storage.getItem(key); | ||
} else { | ||
console.warn( | ||
`Attempted to get item "${key}" from localStorage, but localStorage is not available.`, | ||
); | ||
return null; | ||
} | ||
}, | ||
setItem(key: string, value: string): void { | ||
if (storage) { | ||
storage.setItem(key, value); | ||
} else { | ||
console.warn( | ||
`Attempted to set item "${key}" in localStorage, but localStorage is not available.`, | ||
); | ||
} | ||
}, | ||
removeItem(key: string): void { | ||
if (storage) { | ||
storage.removeItem(key); | ||
} else { | ||
console.warn( | ||
`Attempted to remove item "${key}" from localStorage, but localStorage is not available.`, | ||
); | ||
} | ||
}, | ||
clear(): void { | ||
if (storage) { | ||
storage.clear(); | ||
} else { | ||
console.warn( | ||
"Attempted to clear localStorage, but localStorage is not available.", | ||
); | ||
} | ||
}, | ||
}; | ||
} | ||
Comment on lines
+322
to
+380
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Approved: Implementation of The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved simplification of
getItem
.The
getItem
function is simplified to directly uselocalStorage.getItem
, assuming error handling is managed bysafeLocalStorage
. This change streamlines the code. However, adding a comment explaining this assumption could be beneficial for future maintainers.Consider adding a comment like this:
# Directly using `localStorage.getItem` as `safeLocalStorage` handles errors internally