{"record":{"id":"ab4a7566782b701d","repo":"mantinedev/mantine","slug":"use-local-storage-failed-to-set-value-to-storage","errorCode":null,"errorMessage":"use-local-storage: Failed to set value to storage, localStorage is blocked","messagePattern":"use-local-storage: Failed to set value to storage, localStorage is blocked","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/@mantine/hooks/src/use-local-storage/create-storage.ts","lineNumber":57,"sourceCode":"    return value;\n  }\n}\n\nfunction createStorageHandler(type: StorageType) {\n  const getItem = (key: string) => {\n    try {\n      return window[type].getItem(key);\n    } catch (error) {\n      console.warn('use-local-storage: Failed to get value from storage, localStorage is blocked');\n      return null;\n    }\n  };\n\n  const setItem = (key: string, value: string) => {\n    try {\n      window[type].setItem(key, value);\n    } catch (error) {\n      console.warn('use-local-storage: Failed to set value to storage, localStorage is blocked');\n    }\n  };\n\n  const removeItem = (key: string) => {\n    try {\n      window[type].removeItem(key);\n    } catch (error) {\n      console.warn(\n        'use-local-storage: Failed to remove value from storage, localStorage is blocked'\n      );\n    }\n  };\n\n  return { getItem, setItem, removeItem };\n}\n\nexport type UseStorageReturnValue<T> = [\n  T, // current value","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/mantinedev/mantine/blob/8a284e2c2c53a9cb6f39f5dc389bf41b7a2073f8/packages/@mantine/hooks/src/use-local-storage/create-storage.ts#L39-L75","documentation":"A console.warn (not a thrown error) emitted by use-local-storage when writing to window.localStorage throws. The value update is not persisted; the hook's in-memory state still updates normally. It exists to inform developers why values disappear after a reload when storage is blocked.","triggerScenarios":"window.localStorage.setItem(key, value) throws — blocked storage (privacy settings, third-party context/iframe, Safari private mode), storage quota exceeded (QuotaExceededError), or SSR where localStorage is undefined. Emitted on every setValue call (including the initial write of the default value).","commonSituations":"Apps in third-party iframes with cookies blocked; Safari private mode; storing large objects (base64 images, big JSON) until the ~5MB quota is exceeded; privacy-focused browsers (Brave shields); automated tests with incomplete localStorage mocks.","solutions":["Confirm storage is blocked vs quota exceeded by inspecting the caught error (QuotaExceededError means reduce data size)","Serialize smaller values or add an LRU/cap before writing large state to useLocalStorage","If in an iframe, use the Storage Access API or move to first-party context","Treat as expected degradation in privacy contexts — the hook still functions without persistence"],"exampleFix":"// If quota is the issue, trim what you persist\nconst value = useLocalStorage({\n  key: 'draft',\n  defaultValue: '',\n});\nsetLocalStorageValue(value.slice(0, 1000)); // avoid QuotaExceededError","handlingStrategy":"fallback","validationCode":"function canWriteLocalStorage(): boolean {\n  try {\n    const k = '__w__';\n    window.localStorage.setItem(k, 'x');\n    window.localStorage.removeItem(k);\n    return true;\n  } catch {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"// If you persist large values yourself, guard the write:\ntry {\n  localStorage.setItem(key, JSON.stringify(value));\n} catch (e) {\n  if (e instanceof DOMException && e.name === 'QuotaExceededError') {\n    // prune data and retry, or skip persistence\n  }\n}","preventionTips":["Cap the size of values stored via useLocalStorage (avoid base64 blobs)","Store normalized/minimal state, derive the rest","Handle QuotaExceededError separately from blocked-storage errors","Assume persistence may silently fail in privacy modes; surface a UI hint if data loss matters"],"tags":["local-storage","quota","privacy","use-local-storage","warning"],"backgroundTag":"local-storage-blocked","analyzedSha":"8a284e2c2c53a9cb6f39f5dc389bf41b7a2073f8","analyzedAt":"2026-08-28T10:25:51.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}