Clear cache OR clear service worker

I have an extension that modifies headers of some requests when certain page is being loaded.
However some pages are using Service worker to cache resources, and these requests (handled by Service worker) are then not modified by my extension, because they are coming from the Service worker cache.

Clearing cache helps, but can I do this from extension?

Stopping service worker would help as well, maybe I can do this instead? (let’s assume in this case that the service worker is not really needed)

As far as I’m aware service workers are still a pretty unsolved thing to interact with as extension. This includes things like requests it handles, or trying to interact with it etc.

1 Like

You can clear cache and service workers using the browsingData api… Not sure though how this affects currently active service workers:

1 Like

Thank you! This looks like the right API for the job…
But it requires new permission browsingData (sadly not available as Optional permission).

Also I’m a bit confused about functionality of this API, it seems to be only partially implemented in Firefox:

Note that although this function can take a browsingData.RemovalOptions object, it will be ignored. The entire cache is always cleared when using this function.

So far I’ve tried to clear the cache using browser.tabs.reload(tabId, {bypassCache: true}); and immediately closing the tab (hacking solution :slight_smile:) but it didn’t worked as I thought at all…

Well, I think the bypassCache probably doesn’t affect service workers, as they might cache differently. Also service workers might use indexeddb. So you might want to try using browsingData.remove with cache: true, indexedDB: true and serviceWorkers: true

All of these options, however clean the entire data, not domain-specific.