Registry / storage / swr-sync-storage

swr-sync-storage

JSON →
library1.0.1jsnpmunverified

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.

npm install swr-sync-storage
INSTALL
IMPORT
SIG · SWR-SYNC-STORAGE
S
swr-sync-storage
storagejavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

syncWithStorage
import { syncWithStorage } from 'swr-sync-storage'
const { syncWithStorage } = require('swr-sync-storage')
ESM import; CJS require might not work if package is ESM-only (check package.json).
syncWithLocalStorage
import { syncWithLocalStorage } from 'swr-sync-storage'
import { syncWithLocalStorage } from 'swr-sync-storage'
No common mistake; directly imported.
syncWithSessionStorage
import { syncWithSessionStorage } from 'swr-sync-storage'
import { syncWithSessionStorage } from 'swr-sync-storage'
No common mistake; directly imported.

Import and call syncWithLocalStorage to synchronize SWR cache with localStorage.

import { syncWithLocalStorage } from 'swr-sync-storage'; // Initialize synchronization with localStorage const unsubscribe = syncWithLocalStorage(); // Optionally, you can pass a custom parser for non-object values const customParser = (value: string) => { try { return JSON.parse(value); } catch { return value; } }; syncWithLocalStorage(customParser); // To stop syncing, call the returned function // unsubscribe();
Debug
Known issues
gotchaRequires SWR v0.2.0-beta.0 or greater; older versions of SWR are not compatible.
fix
Ensure SWR version matches the peer dependency range.
affects: >=0.0.0
gotchaThe package may be ESM-only; using CommonJS require() might fail.
fix
Use ES module import syntax (import ... from 'swr-sync-storage').
affects: >=1.0.0
gotchaCustom parser function must handle the 'undefined' string case (default parser checks value === 'undefined').
fix
Include a check for 'undefined' in your custom parser or override with a safe JSON.parse.
affects: >=1.0.0
Errors
Common errors & fixes
Uncaught TypeError: Cannot read properties of undefined (reading 'useRef')
SWR version too old (< 0.2.0-beta.0) or not installed.
fix
Install SWR with version ^0.2.0-beta.0: npm install swr@^0.2.0-beta.0
ReferenceError: exports is not defined in ES module scope
Using require() on an ESM-only package.
fix
Change to import statement: import { syncWithStorage } from 'swr-sync-storage'
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
swrrequiredPeer dependency; SWR v^0.2.0-beta.0 required for synchronization to work
Agent activity
25 hits · last 30 days
node
22
Amazon
1
OpenAI (training)
1
Resources
swr-sync-storage — npm install swr-sync-storage · libregistry