{"id":46567,"library":"swr-sync-storage","title":"swr-sync-storage","description":"Synchronize SWR cache with localStorage or sessionStorage to provide offline cache persistence. Version 1.0.1, stable release. Requires SWR v0.2.0-beta.0 or greater as a peer dependency. This package allows binding SWR's cache to browser storage (localStorage or sessionStorage), enabling offline support and cache persistence across sessions. It provides three importable functions: syncWithStorage (generic, takes a storage type string), syncWithLocalStorage (pre-bound to localStorage), and syncWithSessionStorage (pre-bound to sessionStorage). Unlike manual cache management, it automatically syncs SWR cache changes to storage and restores cache on reload. Supports custom JSON parsing for non-object values. TypeScript types included.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","typescript"],"install":[{"cmd":"npm install swr-sync-storage","lang":"bash","label":"npm"},{"cmd":"yarn add swr-sync-storage","lang":"bash","label":"yarn"},{"cmd":"pnpm add swr-sync-storage","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; SWR v^0.2.0-beta.0 required for synchronization to work","package":"swr","optional":false}],"imports":[{"note":"ESM import; CJS require might not work if package is ESM-only (check package.json).","wrong":"const { syncWithStorage } = require('swr-sync-storage')","symbol":"syncWithStorage","correct":"import { syncWithStorage } from 'swr-sync-storage'"},{"note":"No common mistake; directly imported.","wrong":"import { syncWithLocalStorage } from 'swr-sync-storage'","symbol":"syncWithLocalStorage","correct":"import { syncWithLocalStorage } from 'swr-sync-storage'"},{"note":"No common mistake; directly imported.","wrong":"import { syncWithSessionStorage } from 'swr-sync-storage'","symbol":"syncWithSessionStorage","correct":"import { syncWithSessionStorage } from 'swr-sync-storage'"}],"quickstart":{"code":"import { syncWithLocalStorage } from 'swr-sync-storage';\n\n// Initialize synchronization with localStorage\nconst unsubscribe = syncWithLocalStorage();\n\n// Optionally, you can pass a custom parser for non-object values\nconst customParser = (value: string) => {\n  try {\n    return JSON.parse(value);\n  } catch {\n    return value;\n  }\n};\nsyncWithLocalStorage(customParser);\n\n// To stop syncing, call the returned function\n// unsubscribe();","lang":"typescript","description":"Import and call syncWithLocalStorage to synchronize SWR cache with localStorage."},"warnings":[{"fix":"Ensure SWR version matches the peer dependency range.","message":"Requires SWR v0.2.0-beta.0 or greater; older versions of SWR are not compatible.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use ES module import syntax (import ... from 'swr-sync-storage').","message":"The package may be ESM-only; using CommonJS require() might fail.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Include a check for 'undefined' in your custom parser or override with a safe JSON.parse.","message":"Custom parser function must handle the 'undefined' string case (default parser checks value === 'undefined').","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.1':18 'across':49 'allow':32 'automat':81 'bind':33 'bound':66,73 'browser':38 'cach':7,15,36,47,78,84,90 'chang':85 'custom':94 'depend':29 'enabl':43 'function':55 'generic':57 'greater':25 'import':54 'includ':104 'javascript':105 'json':95 'localstorag':9,40,68 'manag':79 'manual':77 'non':99 'non-object':98 'object':100 'offlin':14,44 'packag':31 'pars':96 'peer':28 'persist':16,48 'pre':65,72 'pre-bound':64,71 'provid':13,52 'releas':20 'reload':92 'requir':21 'restor':89 'session':50 'sessionstorag':11,42,75 'stabl':19 'storag':4,39,60,87 'string':62 'support':45,93 'swr':2,6,22,34,83 'swr-sync-storag':1 'sync':3,82 'synchron':5 'syncwithlocalstorag':63 'syncwithsessionstorag':70 'syncwithstorag':56 'take':58 'three':53 'type':61,103 'typescript':102,106 'unlik':76 'v0.2.0-beta.0':23 'valu':101 'version':17","created_at":"2026-06-07T13:00:33.528263+00:00","updated_at":"2026-06-07T13:00:33.528263+00:00","problems":[{"fix":"Install SWR with version ^0.2.0-beta.0: npm install swr@^0.2.0-beta.0","cause":"SWR version too old (< 0.2.0-beta.0) or not installed.","error":"Uncaught TypeError: Cannot read properties of undefined (reading 'useRef')"},{"fix":"Change to import statement: import { syncWithStorage } from 'swr-sync-storage'","cause":"Using require() on an ESM-only package.","error":"ReferenceError: exports is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":null,"npm":"swr-sync-storage","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}