Files
supabase/packages/common/safe-storage.ts
Ali Waseem 1c2d28d5b3 chore: wrap local storage into helper methods that are safer (#46628)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

- Noticing our code we have many patterns of calling localstorage and
handling those errors
- We should add those in a single well tested file
- Handle those errors in the singleton which makes it easier for us to
debug customer issues. Logger is outputing local storage warnings for
feature we expose
- Side effect of this is random crashes on studio when local storage
isn't available or handled correctly

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Refactor**
* Improved browser storage handling across the app for more reliable
persistence and graceful behavior in restricted or non-browser
environments (settings, previews, charts, tabs, sign-in/session flows,
integrations, and UI state).

* **New Features**
* Introduced a safe storage layer to standardize and harden
local/session persistence.

* **Tests**
  * Added comprehensive tests covering the new safe storage behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-04 07:41:28 -06:00

85 lines
2.2 KiB
TypeScript

export type StorageKind = 'local' | 'session'
// [Ali] Dedupe warnings so a fully-blocked environment doesn't flood the console with
// the same message on every read/write. Keyed by kind + action + storage key.
const warnedKeys = new Set<string>()
function reportFailure(kind: StorageKind, action: string, key: string, error: unknown) {
const dedupeKey = `${kind}:${action}:${key}`
if (warnedKeys.has(dedupeKey)) return
warnedKeys.add(dedupeKey)
console.warn(
`[safe-storage] ${kind}Storage.${action}("${key}") failed; continuing without persistence.`,
error
)
}
function getBackingStore(kind: StorageKind): Storage | null {
if (typeof window === 'undefined') return null
try {
return kind === 'local' ? window.localStorage : window.sessionStorage
} catch {
return null
}
}
function createSafeStorage(kind: StorageKind) {
return {
getItem(key: string): string | null {
const store = getBackingStore(kind)
if (store === null) return null
try {
return store.getItem(key)
} catch (error) {
reportFailure(kind, 'getItem', key, error)
return null
}
},
setItem(key: string, value: string): void {
const store = getBackingStore(kind)
if (store === null) return
try {
store.setItem(key, value)
} catch (error) {
reportFailure(kind, 'setItem', key, error)
}
},
removeItem(key: string): void {
const store = getBackingStore(kind)
if (store === null) return
try {
store.removeItem(key)
} catch (error) {
reportFailure(kind, 'removeItem', key, error)
}
},
keys(): string[] {
const store = getBackingStore(kind)
if (store === null) return []
try {
return Object.keys(store)
} catch (error) {
reportFailure(kind, 'keys', '*', error)
return []
}
},
clear(): void {
const store = getBackingStore(kind)
if (store === null) return
try {
store.clear()
} catch (error) {
reportFailure(kind, 'clear', '*', error)
}
},
}
}
export const safeLocalStorage = createSafeStorage('local')
export const safeSessionStorage = createSafeStorage('session')