Skip to content

Cross-tab sync

Status: Stable (0.2)
Import: @jayoncode/storage/cross-tab

Previous: Async · Next: Quota

Notify other same-origin tabs when a sync storage instance writes. Notify-only — no silent remote merge, no multi-tab ACID.

Quick start

ts
import { createStorage, createLocalStorageAdapter } from "@jayoncode/storage";
import { enableCrossTabSync } from "@jayoncode/storage/cross-tab";

const base = createStorage({
  namespace: "app",
  adapter: createLocalStorageAdapter(),
});

const { storage, stop } = enableCrossTabSync(base, {
  onRemote: (event) => {
    // event.type: "set" | "remove" | "clear"
    // Re-read with storage.get(event.key) if you need the value
    refreshUi();
  },
});

storage.set("theme", "dark"); // broadcasts to other tabs
// later:
stop();

Behavior

MechanismRole
BroadcastChannelPrimary (joc-storage:${namespace} by default)
window storage eventExtra signal when using localStorage

Use the returned storage for writes so broadcasts fire. get / peek / has still read locally.

Options

OptionDefaultNotes
channeljoc-storage:${namespace}BroadcastChannel name
onRemoteCallback for peer changes
listenStorageEventstrueToggle storage event listener

Non-goals

  • Applying remote envelopes automatically
  • Syncing IndexedDB async instances (use app-level BroadcastChannel if needed)
  • Replacing Observable (in-process) or Transactions (same-tab)

See also: Async · FAQ

An ecosystem of independent, headless TypeScript libraries engineered for modern web development. Every package includes interactive playgrounds and documentation that evolves alongside the code.