Lesson 6 of 9 · 19m
Extension Storage and State Migration
chrome.storage is small, observable, and versioned application state. Choose the right area, serialize carefully, and migrate schemas like production data.
Extension state feels harmless until the first user updates from version 1.0 to 1.3 and loses every setting. The browser does not know your schema. It stores whatever JSON-compatible values you wrote last year and faithfully hands them back to this year's code. Storage deserves the same migration discipline as a server database, just with tighter limits and stranger timing.
Pick the correct storage area
await chrome.storage.local.set({ schemaVersion: 3, rules: compiledRules });
const sessionToken = await chrome.storage.session.get("oauthState");
await chrome.storage.sync.set({ theme: "high-contrast" });local is the default durable store on one device. sync propagates small user preferences across signed-in browser instances, with tight quotas and no guarantee of immediacy. session lives in memory for the browser session in supporting browsers and is useful for transient secrets or in-flight OAuth state. It is not encrypted secret storage. Never use sync for large caches, per-site history, or binary payloads; a quota failure there often appears only on the user's second machine.
Model storage as a versioned document
const CURRENT_SCHEMA = 3;
async function loadState() {
const raw = await chrome.storage.local.get(null);
const state = migrateState(raw);
if (state.schemaVersion !== raw.schemaVersion) {
await chrome.storage.local.set(state);
}
return state;
}
function migrateState(raw) {
if (!raw.schemaVersion) {
return { schemaVersion: 1, enabled: Boolean(raw.enabled), sites: Array.isArray(raw.hosts) ? raw.hosts : [] };
}
return raw.schemaVersion >= CURRENT_SCHEMA ? raw : applyMigrations(raw);
}The key detail is that migration runs before business logic. Do not scatter shape checks through the UI. Keep migrations pure and test them with fixtures copied from real released versions.
Writes race across contexts
A popup, service worker, and content script can write at the same time. Last-write-wins is the default behavior whether you intended it or not. Read-modify-write operations need a single owner or a transaction-like pattern. Often the cleanest design is: the service worker owns mutation; other contexts send commands; observers render state.
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === "local" && changes.settings) applySettings(changes.settings.newValue);
});Treat onChanged as a notification, not an authoritative synchronized stream. It can arrive after another write has already landed.
Sensitive data needs extra thought
chrome.storage is not a password manager. Tokens stored there are readable to extension code and potentially exposed through extension compromise. Use the least powerful token available, prefer short-lived OAuth tokens, encrypt user-sensitive material with keys that are not stored beside it, and document honestly what is stored.
Field notes
- Storage accepts JSON-compatible values; Dates become strings unless you encode them deliberately.
storage.local.get(null)is useful for migrations but too broad for routine reads — request explicit keys.- Quota errors are operational failures. Surface "not saved" instead of pretending the write worked.
- Keep cache invalidation separate from user settings; a user expects uninstall or reset to affect preferences, not necessarily rebuildable caches.
Was this lesson helpful?