惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
N
Netflix TechBlog - Medium
C
Check Point Blog
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
腾讯CDC
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
V
V2EX
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
B
Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Browser Storage for Extension Developers: localStorage vs...
Weather Cloc · 2026-05-04 · via DEV Community

When building a Firefox extension, you have two main options for client-side storage. Choosing wrong will either lock you out of sync features or break your extension on private browsing.

Here's the practical difference.

The Short Answer

Use browser.storage.local. Not localStorage. Not sessionStorage. Not indexedDB (unless you have very specific needs).

Here's why.

Why Not localStorage?

In a regular web page, localStorage is straightforward. But in an extension:

It doesn't work in background scripts.

// In a background script — THIS WILL FAIL
localStorage.setItem('key', 'value'); // ReferenceError: window is not defined

Enter fullscreen mode Exit fullscreen mode

Background scripts don't have a window object. They run in a service worker context (MV3) or background page context (MV2), neither of which has localStorage.

It doesn't sync across devices.

Even if you use localStorage in a content script or popup, it's device-local only. browser.storage.sync can sync across Firefox installations.

Private browsing isolation.

In Firefox, extensions can be configured to run in private browsing. localStorage in private windows is isolated and cleared when the private window closes. browser.storage.local persists regardless.

browser.storage.local API

The API is promise-based and consistent across contexts:

// Save data
await browser.storage.local.set({
  theme: 'dark',
  city: 'San Francisco',
  clocks: [
    { zone: 'America/New_York', label: 'NYC' },
    { zone: 'Europe/London', label: 'London' }
  ]
});

// Read data
const prefs = await browser.storage.local.get(['theme', 'city', 'clocks']);
console.log(prefs.theme); // 'dark'

// Read all
const allPrefs = await browser.storage.local.get(null);

// Remove a key
await browser.storage.local.remove('city');

// Clear everything
await browser.storage.local.clear();

Enter fullscreen mode Exit fullscreen mode

browser.storage.sync

For data you want to sync across the user's Firefox installations:

// Save to sync storage
await browser.storage.sync.set({ theme: 'dark' });

// Read from sync storage
const prefs = await browser.storage.sync.get('theme');

Enter fullscreen mode Exit fullscreen mode

Limitations:

  • 100KB maximum total data
  • 8KB per key
  • 512 items maximum
  • Firefox Sync must be enabled

For Weather & Clock Dashboard, I chose storage.local because:

  1. Settings (city, timezones) are personal to the device
  2. The user might have different setups on different machines
  3. No Firefox Sync dependency

Listening for Changes

A useful pattern for reactive UIs:

browser.storage.onChanged.addListener((changes, areaName) => {
  if (areaName === 'local') {
    if (changes.theme) {
      applyTheme(changes.theme.newValue);
    }
    if (changes.city) {
      fetchWeather(changes.city.newValue);
    }
  }
});

Enter fullscreen mode Exit fullscreen mode

This lets different parts of your extension react to storage changes without direct coupling.

Storage Size Limits

browser.storage.local has no limit by default, but requesting the unlimitedStorage permission removes any browser-imposed quota:

// manifest.json
"permissions": ["storage", "unlimitedStorage"]

Enter fullscreen mode Exit fullscreen mode

For most extensions, the default is sufficient. Only needed for extensions storing large amounts of data.

Pattern: Settings with Defaults

const DEFAULTS = {
  theme: 'light',
  city: 'New York',
  clocks: [{ zone: 'America/New_York', label: 'Local' }]
};

async function getSettings() {
  const stored = await browser.storage.local.get(Object.keys(DEFAULTS));
  return { ...DEFAULTS, ...stored }; // stored values override defaults
}

Enter fullscreen mode Exit fullscreen mode

This lets you add new settings with defaults without breaking existing users who don't have the new keys.

Practical Example

Weather & Clock Dashboard stores all user preferences this way:

// Load settings on new tab open
const settings = await browser.storage.local.get(null);
const city = settings.city || 'New York';
const clocks = settings.clocks || DEFAULT_CLOCKS;
const theme = settings.theme || 'light';

applyTheme(theme);
renderClocks(clocks);
fetchWeather(city);

Enter fullscreen mode Exit fullscreen mode

Simple, reliable, works everywhere the extension runs.


Source code: github.com/oren-sys/weather-clock-dashboard