Making a Firefox Extension Work Offline — Service Workers vs. Cache API
A common question when building browser extensions: how do you handle offline or spotty network conditions? Here is what I learned building the Weather & Clock Dashboard .
The Problem
The extension fetches weather data from an external API. If the user opens a new tab with no internet connection, they should see the last known weather, not an error.
Option 1: Service Workers
Service workers can intercept network requests and serve cached responses. But in MV3 Firefox extensions, service workers have limitations — they are not persistent and can be terminated.
// manifest.json " background " : { " service_worker " : " background.js " }
Enter fullscreen mode
Exit fullscreen mode
Option 2: browser.storage.local (What I Used)
For an extension that just needs to cache the last API response, browser.storage.local is simpler and more reliable than a service worker:
async function getWeather ( city ) { const CACHE_KEY = '…