@livestore/adapter-web runs LiveStore in a browser. It provides:
makePersistedAdapter— persisted storage in the Origin Private File System (OPFS) with SharedWorker-based multi-tab leader electionmakeInMemoryAdapter— ephemeral in-memory storage (no persistence across reloads)makeSingleTabAdapter— persisted storage without SharedWorker (for browsers that don’t support it)
Installation
- npm
- pnpm
- yarn
Exports
makePersistedAdapter
Creates a web adapter with persistent OPFS storage. Requires a dedicated worker and a shared worker. On browsers without SharedWorker support (e.g. Android Chrome), it automatically falls back to single-tab mode.Parameters
Constructor or factory for the dedicated worker. In Vite, use the
?worker import:Constructor or factory for the shared worker. Import from
@livestore/adapter-web/shared-worker:Where to persist data. Currently only OPFS is supported:
Wipes all persisted data for this store on startup. Use only during development.
Stable identifier for this browser client. Defaults to a random ID stored in
localStorage (shared across tabs).Identifier for this browser tab/window. Defaults to a random ID stored in
sessionStorage (unique per tab).Worker file setup
Create alivestore.worker.ts file that calls makeWorker:
livestore.worker.ts
Full example
app.ts
makeInMemoryAdapter
Creates a web adapter with no persistence. Data is lost on page reload. Ideal for tests, demos, and sandboxes.Parameters
Optional sync backend. Useful when you want real-time collaboration without persistence.
Pre-populate the in-memory database from an existing snapshot.
Defaults to a random NanoID.
Defaults to a random NanoID.
Example
canUseSharedWorker
Returnstrue when the SharedWorker API is available in the current browser context. Returns false on Android Chrome and other browsers that don’t support it.
makePersistedAdapter calls this internally and falls back to makeSingleTabAdapter automatically. Use it directly only if you need to branch UI behaviour based on SharedWorker availability.
Vite configuration
If you use the Vite development server, import the polyfill in your worker file to ensure hot-reloading works correctly:livestore.worker.ts
The
storage: { type: 'opfs' } option requires the browser to support the Origin Private File System API. LiveStore falls back to in-memory storage automatically in private browsing mode and other contexts where OPFS is unavailable, with a console warning.Peer dependencies
| Package | Version |
|---|---|
effect | ^3.19.19 |