Skip to content

Commit

Permalink
fix: default storage setItem (#4472)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm authored Dec 20, 2024
1 parent 568c905 commit 3892ebd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-carrots-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Added handling to default storage for `setItem` errors, like `QuotaExceededError`, `SecurityError`, etc.
11 changes: 6 additions & 5 deletions packages/core/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import type {
} from './connectors/createConnector.js'
import { injected } from './connectors/injected.js'
import { type Emitter, type EventData, createEmitter } from './createEmitter.js'
import { type Storage, createStorage, noopStorage } from './createStorage.js'
import {
type Storage,
createStorage,
getDefaultStorage,
} from './createStorage.js'
import { ChainNotConfiguredError } from './errors/config.js'
import type {
Compute,
Expand All @@ -43,10 +47,7 @@ export function createConfig<
const {
multiInjectedProviderDiscovery = true,
storage = createStorage({
storage:
typeof window !== 'undefined' && window.localStorage
? window.localStorage
: noopStorage,
storage: getDefaultStorage(),
}),
syncConnectedChain = true,
ssr = false,
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/createStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ export const noopStorage = {
setItem: () => {},
removeItem: () => {},
} satisfies BaseStorage

export function getDefaultStorage() {
const storage = (() => {
if (typeof window !== 'undefined' && window.localStorage)
return window.localStorage
return noopStorage
})()
return {
...storage,
setItem(key, value) {
try {
storage.setItem(key, value)
// silence errors by default (QuotaExceededError, SecurityError, etc.)
} catch {}
},
} satisfies BaseStorage
}

0 comments on commit 3892ebd

Please sign in to comment.