localstorage
JSON-aware wrappers around localStorage, with an availability check and
safe no-ops when storage is unavailable (private browsing, quota, etc.).
Import: @rtorcato/browser-common/localstorage
๐ MDN: Web Storage API ยท ๐ caniuse: namevalue-storage
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
Window.localStorage | โ 4+ | โ 12+ | โ 3.5+ | โ 4+ | โ 3.2+ | Widely available |
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Window.localStorage
Exampleโ
import {
isLocalStorageAvailable,
setLocalStorage,
getLocalStorage,
removeLocalStorage,
} from '@rtorcato/browser-common/localstorage'
if (isLocalStorageAvailable()) {
setLocalStorage('prefs', { theme: 'dark' })
const prefs = getLocalStorage('prefs') as { theme: string } | null
removeLocalStorage('prefs')
}
Exportsโ
isLocalStorageAvailable()โ feature check, verified with a write/remove probesetLocalStorage(key, value)โ JSON-stringifies and storesvaluegetLocalStorage(key)โ parses and returns the stored value, ornull(caller must narrow the type)removeLocalStorage(key)โ removes a single keyclearLocalStorage()โ clears all keys
See the API reference for full signatures.