On 18/07/2023 01:59, Kein Zantezuken wrote:
>
> is that because offscreen page and, therefore, its script(s) are the
> only close-to-persistent thing we can have,
I am curious why offscreen is a better storage than `storage.session`
https://developer.chrome.com/docs/extensions/reference/storage/#storage-areas
It is asynchronous and JSON serialization is involved due to messaging.
> So I NEED it to be created at once when service_worker
> starts, not before any of the tabs are closed and the relevant event is
> dispatched
Sorry, I do not know you use case. From my point of view it should be
possible to implement lazy initialization with a global variable holding
a `Promise` to the result of the initialization function. With such
approach it should not matter whether the code is executed on loading of
the service worker or in response to event handler. Even if you still
prefer to call a function at the loading time then save the returned
promise to a variable and `await` for it in event handlers.
> > Since onClosed
>
> Can you elaborate on this? I can't find it in API, is there a way to
> know when service_worker is being/about to reloaded via extension page?
My point was that you should not experience any issues during manual
testing. When you click on the extension reloading button, there is some
time interval between service worker loading and closing a tab. It
should be enough to create the offscreen document. When the service
worker is stopped, the offscreen document persists and ready to react to
messages. The only exception is cold start.
I do not think there is API that allows to execute some code before
reloading, but during loading you may test whether it is
installation/update or resuming, see
- serviceWorker.state
- self.addEventListener("activate", /*...*/)