localStorage
@homeostate/persist-local-storage
localStorage persistence adapter for @homeostate/core
localStorage storage for createPersistence from
@homeostate/core. It keeps a Yjs, Loro or Automerge
document in the browser, so its state survives reloads, offline starts and every peer leaving
the room.
Install
npm install @homeostate/core @homeostate/persist-local-storage @homeostate/crdt-yjs yjsUsage
import * as Y from "yjs";
import { createPersistence, createSyncEngine } from "@homeostate/core";
import { createYjsBackend, createYjsPersistable } from "@homeostate/crdt-yjs";
import { createLocalStorageAdapter } from "@homeostate/persist-local-storage";
const doc = new Y.Doc();
const persistence = createPersistence(
createYjsPersistable(doc),
createLocalStorageAdapter(),
{ key: "todos" },
);
await persistence.whenLoaded;
createSyncEngine(createYjsBackend(doc, "shared"), adapter).connect();Connect the engine after whenLoaded, so it adopts the restored state instead of seeding the
store's defaults over it. See the persistence guide for the whole flow.
Options
createLocalStorageAdapter({
prefix: "homeostate:", // prepended to every key
storage: sessionStorage, // defaults to localStorage
});Limits
Each document is one JSON entry holding its updates in base64, rewritten on every update.
Browsers allow about 5 MB per origin; a write past that fails and is reported through
createPersistence's onError. For larger documents use
@homeostate/persist-indexeddb.