Storage

Persist data that should survive surface closes and keep every open surface synchronized when it changes.

Read and write

import {
  getStorageValue,
  setStorageValue,
  removeStorageValue,
} from "@/shared/storage";

await setStorageValue("draft", { title: "Hello" });
const draft = await getStorageValue<{ title: string }>("draft");
await removeStorageValue("draft");

Update with a callback

Update a stored value from its current state without repeating the read-then-write flow.

import { updateStorageValue } from "@/shared/storage";

await updateStorageValue<number>("credits", (current) => (current ?? 0) + 1);

Subscribe to changes

Subscribe when a popup, side panel, options page, or background worker should react to the same key.

import { subscribeToStorageKey } from "@/shared/storage";

const unsubscribe = subscribeToStorageKey("credits", (value) => {
  console.log("credits changed:", value);
});

// Later: unsubscribe();

When to use storage vs messaging

Use storage for durable state such as settings, drafts, and cached results. Use messaging for commands and one-time requests.