I am in the process of converting a Chrome extension from manifest version 2 (MV2) to manifest version 3 (MV3). In the MV2 version, the background page script has a prominent role in the extension: At startup, the background script reads in a large amount of data from IndexedDB to RAM, and then (during operation) handles queries from content scripts injected into the pages that the user visits. To move to MV3, the functionality provided by the MV2 background script now needs to be translated to be performed by a MV3 service worker instead.
Because reading the data from IndexedDB to RAM is slow (relatively speaking), it is not feasible to do this dynamically upon a query from a content script; rather it must be performed by a service worker only once and then the data kept around in order to serve the incoming requests from the content scripts. Thus, the service worker should be persistent (and not be terminated by the browser); for this there exists at least one solution: https://stackoverflow.com/a/66618269/4432991
However, in some cases (I have not been able to pin-point exactly under what circumstances) when a content script sends a message to the service worker, Chrome starts up a second copy of the service worker, even while the first one is still alive, and the two service workers co-exist for some duration. Is there some way to avoid this, and ensure that if a service worker is still alive (in memory), no activity (by content scripts) will cause a second copy of the service worker to be created, and rather all requests by the content scripts will thus be handled by the previously existing service worker?
Thanks for any help!