@statesync/persistence / createLocalStorageBackend
Function: createLocalStorageBackend()
function createLocalStorageBackend<T>(options): StorageBackend<T>;Defined in: persistence/src/storage/local-storage.ts:89
Creates a StorageBackend that persists snapshot data using the browser's localStorage API.
Data stored in localStorage persists across browser sessions and tab closures, making it suitable for long-lived application state. The storage is synchronous under the hood but exposed through an async interface for API consistency.
Browser compatibility: Supported in all modern browsers. Requires a secure context (HTTPS) in some browsers for full functionality. Not available in Web Workers or Service Workers.
Storage limits: localStorage has a ~5MB per-origin limit in most browsers. For larger data, consider using createIndexedDBBackend instead.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the state data stored within snapshot envelopes. |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | LocalStorageBackendOptions | Configuration options for the localStorage backend. |
Returns
A StorageBackend instance backed by localStorage.
Throws
Throws a descriptive error wrapping QuotaExceededError when the localStorage quota is exceeded during a save operation.
Throws
Throws a descriptive error when stored data cannot be deserialized during a load operation (e.g., corrupted or incompatible data).
Examples
const storage = createLocalStorageBackend<MyState>({ key: 'my-app-state' });
// Save a snapshot
await storage.save({ revision: '1', data: { count: 42 } });
// Load the snapshot
const snapshot = await storage.load();
console.log(snapshot?.data.count); // 42
// Clear stored data
await storage.clear();const storage = createLocalStorageBackend<MyState>({
key: 'my-app-state',
serialize: (snapshot) => btoa(JSON.stringify(snapshot)),
deserialize: (data) => JSON.parse(atob(data)),
});