Restart event on manifest v3 extensions

299 views
Skip to first unread message

Asaf Shochet

unread,
Dec 5, 2022, 2:26:38 AM12/5/22
to Chromium Extensions
Hi there,
I am migrating an extension from v2 to v3,
and I see that things are happening when the background service worker is restarting.

So a few questions (Thanks in advance!):

1. What is the event I should register to in order to execute code once the service worker is restarted (already installed)

2. Is the code in background.js running every time the service worker is waking up, and what does this mean about event / message listeners, are they already there on should I add them again, or check for existence before?

Thank you!
Asaf

Jackie Han

unread,
Dec 5, 2022, 2:49:41 AM12/5/22
to Asaf Shochet, Chromium Extensions
It is the same as the event page in MV2.

top-level / first event-loop code runs every time when the service worker starts.

listeners need to register at the top-level / first event-loop in service worker. When the service worker wakes up, listeners are already there.

When you modify service worker code, you need to click the reload button to take effect.


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/fc9e2850-d7ea-4b11-924c-e3eabbfb97c0n%40chromium.org.

Asaf Shochet

unread,
Dec 5, 2022, 3:39:17 AM12/5/22
to Jackie Han, Chromium Extensions
Thank you,
so the event handlers are not removed when the service worker becomes inactive?

wOxxOm

unread,
Dec 5, 2022, 3:40:08 AM12/5/22
to Chromium Extensions, Jackie Han, Chromium Extensions, asaf.s...@evinced.com
> What is the event I should register to in order to execute code once the service worker is restarted (already installed)

None. Just run the code at the beginning of the script.

> event / message listeners - are they already there

No, there's nothing there yet, it's an entirely pristine JS environment before the script runs.

> on should I add them again

Yes.

Here's what happens when the background script starts:
  1. It runs entirely.
  2. When it calls addListener the API stores the function reference internally.
  3. After the last statement has finished executing, the API calls the function(s) for the event that started the background script.
  4. When the internal lifetime timer expires the background script is terminated - it's conceptually same as closing of a tab i.e. JS environment entirely vanishes.

The wake-up is implemented by addListener remembering the event names and their filters (e.g. in webRequest of webNavigation) in the internal database of the browser process, which happens every time the background script runs (but only in the first turn of the event loop). When an event matching the criteria occurs in the browser, the browser starts the extension's background script and the above cycle repeats.

wOxxOm

unread,
Dec 5, 2022, 3:40:51 AM12/5/22
to Chromium Extensions, wOxxOm, Jackie Han, Chromium Extensions, asaf.s...@evinced.com
> so the event handlers are not removed when the service worker becomes inactive?

They are removed.

Jackie Han

unread,
Dec 5, 2022, 3:55:31 AM12/5/22
to Asaf Shochet, Chromium Extensions
so the event handlers are not removed when the service worker becomes inactive?

Yes, you can think so (already there).
As wOxxOm detailed explanation, the browser 1) remember it. 2) remove it. 3) add it. 4) call it.

Asaf Shochet

unread,
Dec 5, 2022, 4:20:38 AM12/5/22
to Jackie Han, Chromium Extensions
Thank you all for answering my questions :)

What I don't understand is that if the event listeners and message handlers are removed when it goes inactive,
what happens when I'll send it messages and it's asleep.

IIUC - it should wake up -> perform it's init script -> and only then listen to the messages?
(I assume there's a queue of pending messages at this moment, send to the worker, pending for its initiation cycle to complete)

Jackie Han

unread,
Dec 5, 2022, 4:32:06 AM12/5/22
to Asaf Shochet, Chromium Extensions, wOxxOm
Correct.

PS: events and messages are not only sent to service worker, they are sent to all extension pages and the service worker who listens to them. For messages, it sends message to all except sender itself.

Asaf Shochet

unread,
Dec 5, 2022, 4:38:48 AM12/5/22
to Jackie Han, Chromium Extensions, wOxxOm
Thank you Jackie and Mr/s wOxxOm,
You are the kings of this forum!
Reply all
Reply to author
Forward
0 new messages