Skip to main content

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

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 probe
  • setLocalStorage(key, value) โ€” JSON-stringifies and stores value
  • getLocalStorage(key) โ€” parses and returns the stored value, or null (caller must narrow the type)
  • removeLocalStorage(key) โ€” removes a single key
  • clearLocalStorage() โ€” clears all keys

See the API reference for full signatures.