diff --git a/.changeset/nine-carrots-reply.md b/.changeset/nine-carrots-reply.md new file mode 100644 index 0000000000..f26df44929 --- /dev/null +++ b/.changeset/nine-carrots-reply.md @@ -0,0 +1,5 @@ +--- +"@wagmi/core": patch +--- + +Added handling to default storage for `setItem` errors, like `QuotaExceededError`, `SecurityError`, etc. diff --git a/packages/core/src/createConfig.ts b/packages/core/src/createConfig.ts index b8be9ce108..154eef5f6d 100644 --- a/packages/core/src/createConfig.ts +++ b/packages/core/src/createConfig.ts @@ -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, @@ -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, diff --git a/packages/core/src/createStorage.ts b/packages/core/src/createStorage.ts index 06854c0b55..1bed370355 100644 --- a/packages/core/src/createStorage.ts +++ b/packages/core/src/createStorage.ts @@ -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 +}