Menu

1 / 2

Making a Firefox Extension Work Offline — Service Workers vs. Cache API

DEV Community·Weather Clock Dash·6 months ago
#lcBawoLj
Reading 0:00
15s threshold

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 = '…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.