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

推荐订阅源

T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
量子位
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园 - Franky
罗磊的独立博客
宝玉的分享
宝玉的分享
博客园_首页
腾讯CDC
The GitHub Blog
The GitHub Blog
D
DataBreaches.Net
IT之家
IT之家
D
Docker
Microsoft Security Blog
Microsoft Security Blog
博客园 - 司徒正美
V
V2EX
月光博客
月光博客
N
Netflix TechBlog - Medium
爱范儿
爱范儿
I
InfoQ
P
Proofpoint News Feed

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
Handling Offline Mode in Firefox Browser Extensions
Weather Cloc · 2026-05-04 · via DEV Community

Handling Offline Mode in Firefox Browser Extensions

When building extensions that depend on network data — like my Weather & Clock Dashboard — offline handling is critical. Users notice immediately when your extension shows stale data or broken UI.

The Problem

Browser extensions run at browser startup. If the user is on a train, airplane, or just has spotty WiFi, your fetch calls will fail. Without proper handling, users see errors or empty states.

Detecting Online Status

The browser gives you two ways to check connectivity:

// Passive check
if (!navigator.onLine) {
  showCachedData();
  return;
}

// Listen for changes
window.addEventListener('online', () => {
  console.log('Back online — refreshing data');
  fetchFreshData();
});

window.addEventListener('offline', () => {
  console.log('Gone offline — switching to cache');
  showOfflineIndicator();
});

Enter fullscreen mode Exit fullscreen mode

Note: navigator.onLine only tells you if you have a network connection — not if that connection actually works. You can be "online" but unable to reach your API.

A Robust Fetch Wrapper

Here's a pattern I use for all API calls in the extension:

async function fetchWithFallback(url, options = {}) {
  const CACHE_KEY = `cache_${url}`;
  const CACHE_TTL_KEY = `cache_ttl_${url}`;
  const TTL_MS = 3600000; // 1 hour

  try {
    // Attempt network fetch with timeout
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), 5000); // 5s timeout

    const response = await fetch(url, {
      ...options,
      signal: controller.signal
    });
    clearTimeout(timeoutId);

    if (!response.ok) throw new Error(`HTTP ${response.status}`);

    const data = await response.json();

    // Cache the fresh data
    const cacheEntry = {
      data,
      timestamp: Date.now()
    };
    localStorage.setItem(CACHE_KEY, JSON.stringify(cacheEntry));

    return { data, fromCache: false };

  } catch (error) {
    console.warn(`Fetch failed for ${url}:`, error.message);

    // Try cache
    const cached = localStorage.getItem(CACHE_KEY);
    if (cached) {
      const { data, timestamp } = JSON.parse(cached);
      const age = Date.now() - timestamp;
      const ageHours = Math.round(age / 3600000);

      return {
        data,
        fromCache: true,
        cacheAge: age,
        stale: age > TTL_MS
      };
    }

    // No cache available
    throw new Error('No data available (offline and no cache)');
  }
}

Enter fullscreen mode Exit fullscreen mode

Showing Stale Data Indicators

Users prefer seeing old data over an error. But they deserve to know it's stale:

async function updateWeather() {
  const statusEl = document.getElementById('weather-status');

  try {
    const { data, fromCache, cacheAge } = await fetchWithFallback(WEATHER_API_URL);

    renderWeather(data);

    if (fromCache) {
      const hours = Math.floor(cacheAge / 3600000);
      const minutes = Math.floor((cacheAge % 3600000) / 60000);
      statusEl.textContent = hours > 0 
        ? `Last updated ${hours}h ago (offline)` 
        : `Last updated ${minutes}m ago (offline)`;
      statusEl.className = 'status-stale';
    } else {
      statusEl.textContent = `Updated just now`;
      statusEl.className = 'status-fresh';
    }

  } catch (error) {
    statusEl.textContent = 'Weather unavailable';
    statusEl.className = 'status-error';
  }
}

Enter fullscreen mode Exit fullscreen mode

Using the Service Worker Cache API

For heavier caching needs, consider the Cache API (available in extensions with the right permissions):

const CACHE_NAME = 'weather-data-v1';

async function cacheResponse(url, data) {
  const cache = await caches.open(CACHE_NAME);
  const response = new Response(JSON.stringify(data), {
    headers: { 'Content-Type': 'application/json' }
  });
  await cache.put(url, response);
}

async function getCachedResponse(url) {
  const cache = await caches.open(CACHE_NAME);
  const response = await cache.match(url);
  if (response) {
    return response.json();
  }
  return null;
}

Enter fullscreen mode Exit fullscreen mode

Testing Offline Behavior

Chrome/Firefox DevTools let you simulate offline mode:

  1. Open DevTools → Network tab
  2. Change "No throttling" dropdown to "Offline"
  3. Reload your extension's new tab page

For automated testing:

// In Playwright tests
await context.setOffline(true);
await page.reload();

// Should show cached data
const weatherText = await page.textContent('#weather-temp');
expect(weatherText).not.toBe('');

// Should show stale indicator
const statusText = await page.textContent('#weather-status');
expect(statusText).toContain('offline');

Enter fullscreen mode Exit fullscreen mode

Background Refresh Pattern

For new tab extensions, pre-fetch data in the background script so it's ready before the tab opens:

// background.js
chrome.alarms.create('weatherRefresh', { periodInMinutes: 30 });

chrome.alarms.onAlarm.addListener(async (alarm) => {
  if (alarm.name === 'weatherRefresh') {
    try {
      const city = await getStoredCity();
      const data = await fetchWeatherData(city);
      await browser.storage.local.set({ 
        weatherCache: data,
        weatherCacheTime: Date.now()
      });
    } catch (e) {
      // Silently fail — cache stays valid
    }
  }
});

Enter fullscreen mode Exit fullscreen mode

Then in your new tab page, read from storage first:

async function loadWeather() {
  const { weatherCache, weatherCacheTime } = await browser.storage.local.get(['weatherCache', 'weatherCacheTime']);

  if (weatherCache) {
    // Show cache immediately for instant load
    renderWeather(weatherCache);
    showCacheAge(weatherCacheTime);
  }

  // Then try to refresh in the background
  fetchFreshWeather().then(data => {
    renderWeather(data);
    clearCacheIndicator();
  }).catch(() => {
    // Keep showing cached data
  });
}

Enter fullscreen mode Exit fullscreen mode

This gives users instant perceived performance even on slow connections.

The Weather & Clock Dashboard Approach

In my extension, I use localStorage for persistence with a 1-hour TTL. The offline indicator shows as a small cloud icon next to the temperature when the data is stale.

Try it out: Weather & Clock Dashboard on AMO

What's your approach to offline handling in extensions? Let me know in the comments!


Part of a series on building Firefox browser extensions.

firefox #javascript #webdev #browserextension #pwa